// luogu-judger-enable-o2
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
using namespace std;
inline int read(int f = 1, int x = 0, char ch = ' ')
{
    while(!isdigit(ch = getchar())) if(ch == '-') f = -1;
    while(isdigit(ch)) x = x*10+ch-'0', ch = getchar();
    return f*x;
}
const int N = 1e5+5;
struct Edge
{
    int next, to;
    Edge(int next = 0, int to = 0):next(next), to(to) {};
}edge[N<<1];
int tot, head[N];
void _add(int x, int y) { edge[++tot] = Edge(head[x], y); head[x] = tot; }
void add(int x, int y) { _add(x, y); _add(y, x); }
int n, m, cn, w[N], f[N], fa[N], s[N], p, q, ans;
int find(int x) { return f[x] == x?x:f[x] = find(f[x]); }
void dfs(int x, int f, int c)
{
    fa[x] = f, s[x] = c;
    for(int i = head[x]; i; i = edge[i].next)
    {
        int y = edge[i].to; if(y == f) continue;
        dfs(y, x, -c); s[x] += s[y];
    }
}
int main()
{
    n = read(), m = read(), p = 1;
    for(int x = 1; x <= n; ++x) f[x] = x;
    for(int i = 1; i <= m; ++i)
    {
        int x = read(), y = read();
        if(find(x) == find(y)) p = x, q = y;
        else f[find(y)] = find(x), add(x, y);
    }
    dfs(p, 0, 1);
    if(n == m)
    {
        for(int x = q; x; x = fa[x]) w[++cn] = s[x];
        if(cn&1)
        {
            if(s[p]&1) return puts("-1"), 0;
            for(int x = q; x; x = fa[x]) s[x] -= s[p]>>1;
        } 
        else
        {
            if(s[p]) return puts("-1"), 0;
            sort(w+1, w+1+cn);   
            for(int x = q; x; x = fa[x]) s[x] -= w[cn>>1];
        }
    }
    else if(s[p]) return puts("-1"), 0;
    for(int x = 1; x <= n; ++x) ans += abs(s[x]);
    printf("%d\n", ans);
    return 0;
}