#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;

struct edge {
    int v;
    edge *next;
} pool[3 * N], *h[N];

int n, m, top = -1;
int size[N], ans;
int p[N], col[N], vis[N];
int circle = -1, S, T;
vector <int> V;

inline void addedge(int u, int v) {
    edge *tmp = &pool[++top];
    tmp -> v = v; tmp -> next = h[u]; h[u] = tmp;
}

void dfs_1(int u) {
    size[u] = col[u] = -col[p[u]];
    vis[u] = true;
    for(edge *tmp = h[u]; tmp; tmp = tmp -> next) {
        int v = tmp -> v;
        if(vis[v] && circle == -1 && v != p[u]) {
            S = u;
            T = v;
            circle = abs(col[u] + col[v]) / 2;
            continue;
        }
        if(vis[v]) continue;
        p[v] = u;
        dfs_1(v);
        size[u] += size[v];
    }
    ans += abs(size[u]);
}

void dfs_2(int u) {
    vis[u] = true;
    size[u] = col[u];
    for(edge *tmp = h[u]; tmp; tmp = tmp -> next) {
        int v = tmp -> v;
        if(vis[v]) continue;
        dfs_2(v);
        size[u] += size[v];
    }
    ans += abs(size[u]);
}

void init() {
    int x, y;
    for(int i = 1; i <= m; i++) {
        scanf("%d%d", &x, &y);
        addedge(x, y);
        addedge(y, x);       
    }
    col[0] = -1;
    dfs_1(1);

    if(m == n - 1) {
        if(size[1]) return (void) printf("-1\n");
    }
    else if(circle == 0) {
        if(size[1]) return (void) printf("-1\n");
        x = S;
        while(x != T) V.push_back(size[x]), ans -= abs(size[x]) , x = p[x];
        V.push_back(0);
        sort(V.begin(), V.end());
        y = V[V.size() / 2];
        for(auto x : V) ans += abs(x - y);
    }
    else {
        //for(int i = 1; i <= n; i++) printf("%d ", size[i]); printf("\n");
        if(abs(size[1]) & 1) return (void) printf("-1\n");
        col[S] -= size[1] / 2;
        col[T] -= size[1] / 2;
        ans = abs(size[1]) / 2;
        for(int i = 1; i <= n; i++) vis[i] = false;
        dfs_2(1);
    }
    printf("%d\n", ans);
}

int main() {
    cin >> n >> m;  
    init();
    return 0;
}

//tree?
