#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m, a, b, dp[N], cu, cv, vis[N], f[N], in[N], p[N], c, col[N];
vector<int> g[N];
void dfs(int u, int fa) {
    f[u] = fa;
    vis[u] = 1; col[u] = col[fa] ^ 1;
    dp[u] = col[u] ? 1 : -1;
    for(auto v : g[u]) if(v != fa) {
        if(!vis[v]) dfs(v, u), dp[u] += dp[v];
        else cu = u, cv = v;
    }
}
void tree() {
    if(dp[1] != 0) { printf("-1\n"); return; }
    int ans = 0;
    for(int i = 1; i <= n; ++i) ans += abs(dp[i]);
    printf("%d\n", ans);
}
void even() {
    if(dp[1] != 0) { printf("-1\n"); return; }
    while(cu != cv) p[++c] = dp[cv], in[cv] = 1, cv = f[cv];
    sort(p + 1, p + 1 + c);
    int mid = p[(c + 1) / 2];
    int ans = abs(mid);
    for(int i = 1; i <= n; ++i) if(!in[i]) ans += abs(dp[i]);
    for(int i = 1; i <= c; ++i) ans += abs(p[i] - mid);
    printf("%d\n", ans);
}
void odd() {
    if(dp[1] % 2 != 0) { printf("-1\n"); return; }
    int x = -dp[1] / 2;
    while(cu) dp[cu] += x, cu = f[cu];
    while(cv) dp[cv] += x, cv = f[cv];
    int ans = abs(x);
    for(int i = 1; i <= n; ++i) ans += abs(dp[i]);
    printf("%d\n", ans);
}
int main() {
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= m; ++i) scanf("%d %d", &a, &b), g[a].push_back(b), g[b].push_back(a);
    dfs(1, 0);
    if(m == n - 1) tree();
    else if(col[cu] ^ col[cv]) even();
    else odd();
    return 0;
}
