#include <bits/stdc++.h>
using namespace std;
#define N 500001
#define M 1000001
int fa[N], d[N], f[N], su, sv;
int hd[N], nx[M], e[M], num;
int n, m, ans;
void dfs(int x, int dep) {
    d[x] = dep, f[x] = dep & 1 ? 1 : -1;
    for (int i = hd[x]; i; i = nx[i]) {
        if (e[i] == fa[x]) continue;
        if (d[e[i]]) {
            su = x, sv = e[i];
            continue;
        }
        fa[e[i]] = x;
        dfs(e[i], dep + 1);
        f[x] += f[e[i]];
    }
}
int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1, x, y; i <= m; i++) {
        scanf("%d%d", &x, &y);
        nx[++num] = hd[x], hd[x] = num, e[num] = y;
        nx[++num] = hd[y], hd[y] = num, e[num] = x;
    }
    dfs(1, 1);
    if (m == n - 1) {
        if (f[1]) return (puts("-1"), 0);
        for (int i = 1; i <= n; i++) ans += abs(f[i]);
    } else if ((d[sv] - d[su]) & 1) {
        if (f[1]) return (puts("-1"), 0);
        vector<int> vec;
        while (sv != su) vec.push_back(f[sv]), d[sv] = 0, sv = fa[sv];
        sort(vec.begin(), vec.end());
        int mid = vec[vec.size() / 2];
        ans = abs(mid);
        for (int i = 1; i <= n; i++)
            if (d[i]) ans += abs(f[i]);
        for (auto i : vec) ans += abs(i - mid);
    } else {
        if (f[1] & 1) return (puts("-1"), 0);
        int del = -f[1] / 2;
        ans = abs(del);
        while (su) f[su] += del, su = fa[su];
        while (sv) f[sv] += del, sv = fa[sv];
        for (int i = 1; i <= n; i++) ans += abs(f[i]);
    }
    printf("%d\n", ans);
    return 0;
}
