#include <bits/stdc++.h>
#define G           \
    if (++ip == ie) \
        if (fread(ip = buf, 1, SZ, stdin))
using namespace std;
const int SZ = 1 << 19, N = 1e5 + 9, M = 2 * N;
char buf[SZ], *ie = buf + SZ, *ip = ie - 1;
inline int in()
{
    G;
    while (*ip < '-')
        G;
    int x = *ip & 15;
    G;
    while (*ip > '-')
    {
        x *= 10;
        x += *ip & 15;
        G;
    }
    return x;
}
int h[N], w[M], q[M], f[N], c[N], v[N];
int Getf(int x)
{
    return x == f[x] ? x : f[x] = Getf(f[x]);
}
void Dfs(int x, int op)
{
    c[x] = op;
    for (int y, i = h[x]; i; i = w[i])
        if ((y = q[i]) != f[x])
            f[y] = x, Dfs(y, -op), c[x] += c[y];
}
int main()
{
    int n = in(), m = in(), rt = 1, re = 1, p = 0, ans = 0;
    for (int i = 1; i <= n; ++i)
        f[i] = i;
    for (int p = 0, i = 1; i <= m; ++i)
    {
        int x = in(), y = in();
        if (Getf(x) != Getf(y))
            f[f[x]] = f[y];
        else
        {
            rt = x, re = y;
            continue;
        }
        w[++p] = h[x];
        q[h[x] = p] = y;
        w[++p] = h[y];
        q[h[y] = p] = x;
    }
    f[rt] = 0;
    Dfs(rt, 1);
    if (n == m)
    {

        p = 0;
        for (int i = re; i; i = f[i])
            v[++p] = c[i];
        if (p % 2)
        {
            if (c[rt] % 2)
                return puts("-1"), 0;
            for (int i = re; i; i = f[i])
                c[i] -= c[rt] / 2;
        }
        else
        {
            if (c[rt])
                return puts("-1"), 0;
            sort(v + 1, v + p + 1);
            for (int i = re; i; i = f[i])
                c[i] -= v[p / 2];
        }
    }
    else if (c[rt])
        return puts("-1"), 0;
    for (int i = 1; i <= n; ++i)
        ans += abs(c[i]);
    return cout << ans, 0;
}