#include <bits/stdc++.h>

using namespace std;

#define N 100000
#define pb push_back

int n, m, tot, d[N + 5], fa[N + 5], sum[N + 5], X, Y, cir[N + 5], a[N + 5];
vector<int> G[N + 5];

int find(int x) {
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}

void dfs(int u, int pa) {
    fa[u] = pa;
    sum[u] = (d[u] & 1) ? -1 : 1;
    for (auto v : G[u]) {
        if (v == pa) continue;
        d[v] = d[u] + 1;
        dfs(v, u);
        sum[u] += sum[v];
    }
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i)
        fa[i] = i;
    for (int i = 1, x, y, fx, fy; i <= m; ++i) {
        scanf("%d%d", &x, &y);
        fx = find(x), fy = find(y);
        if (fx == fy) {
            X = x, Y = y;
            continue;
        }
        G[x].pb(y), G[y].pb(x);
        fa[fy] = fx;
    }
    if (X == 0) X = 1;
    // printf("X:%d Y:%d\n", X, Y);
    dfs(X, 0);
    // for (int i = 1; i <= n; ++i)
    //     printf("%d ", sum[i]);
    // printf("\n");
    if (m == n - 1) {
        if (sum[X])
            return puts("-1"), 0;
    }
    else {
        int t = Y;
        cir[++tot] = t;
        while (fa[t]) t = fa[t], cir[++tot] = t;
        // printf("tot:%d\n", tot);
        if (tot & 1) { // 奇环
            if (sum[X] & 1)
                return puts("-1"), 0;
            for (int x = Y; x; x = fa[x])
                sum[x] -= sum[X] >> 1;
        }
        else { // 偶环
            if (sum[X])
                return puts("-1"), 0;
            for (int i = 1; i <= tot; ++i)
                a[i] = sum[cir[i]];
            sort(a + 1, a + tot + 1);
            for (int x = Y; x; x = fa[x])
                sum[x] -= a[tot >> 1];
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i)
        ans += abs(sum[i]);
    printf("%d\n", ans);
    return 0;
}