#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n,m;
vector<int> adjlist[N];
int S, T;
int p[N],col[N], num[N];
int ans = 0;
void dfs2(int u){
    for (auto v : adjlist[u]){
        if (p[v] == u){
            dfs2(v);
            num[u] += num[v];
        }
    }
    ans += abs(num[u]);
}
void dfs1(int u){
    for (auto v : adjlist[u]){
        if (col[v] == -1){
            col[v] = 1^col[u];
            p[v] = u;
            dfs1(v);
        }
        else if (p[u] != v) S = v, T = u;
    }
}
int main(){
    scanf("%d%d",&n,&m);
    for (int i = 0,a,b; i < m; i++){
        scanf("%d%d",&a,&b);
        adjlist[a].push_back(b);
        adjlist[b].push_back(a);
    }
    memset(col,-1,sizeof(col));
    col[1] = 0;
    p[1] = 0;
    dfs1(1);
    for (int i = 1; i <= n; i++){
        num[i] = col[i]?-1:1;
    }
    dfs2(1);
    if (n == m){
        //printf("%d %d\n",S,T);
        if (col[S] != col[T]){
            //printf("even cycle");
            int c = S;
            vector<int> li;
            while (c != T){
                li.push_back(num[c]); c = p[c];
            }
            sort(li.begin(),li.end());
            int x = li[li.size()/2];
            c = S;
            while (c != T){
                num[c] -= x; c = p[c];
            }
            ans = abs(x);
            for (int i = 1; i <= n; i++){
                ans += abs(num[i]);
            }
        }
        else{
            //printf("odd cycle\n");
            int x = num[1]/2;
            //printf("difference is %d\n",num[T]);
            for (int i = 1; i <= n; i++){
                num[i] = col[i]?-1:1;
            }
            num[S] -= x;
            num[T] -= x;
            ans = abs(x);
            dfs2(1);
        }
    }
    if (num[1]) printf("-1");
    else printf("%d",ans);
}
