#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<LL,LL>
#define x first
#define y second
#define pi acos(-1)
#define sqr(x) ((x)*(x))
#define pdd pair<double,double>
#define MEMS(x) memset(x,-1,sizeof(x))
#define MEM(x) memset(x,0,sizeof(x))
#define less Less
#define EPS 1e-4
#define arg ARG
#define cpdd const pdd
#define rank Rank
#define KK 500
#define N 100005
#define MXN 200005
vector<int> v[100005];
LL ans;
int dep[100005];
int val[100005];
int a,b;
LL dfs(int x,int f){
    dep[x]=dep[f]+1;
    LL sum=0;
    for(auto it:v[x]){
        if(it!=f){
            if((it==a&&x==b)||(it==b&&x==a))continue;
            sum+=dfs(it,x);
        }
    }
    if(dep[x]&1)sum++;
    else sum--;
    sum+=val[x];
    ans+=abs(sum);
    return sum;
}
int vis[100005];
vector<int> circle;
vector<int> stk;
int Find;
void dfsc(int x,int f){
    stk.pb(x);
    vis[x]=1;
    for(auto it:v[x]){
        if(it!=f){
            if(vis[it]){
                while(stk.back()!=it){
                    circle.pb(stk.back());
                    stk.pop_back();
                }
                circle.pb(stk.back());
                Find=1;
            }
            else{
                dfsc(it,x);
            }
            if(Find)return;
        }
    }
    if(stk.back()==x)stk.pop_back();
}
int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i = 0;i<m;i++){
        int x,y;
        scanf("%d %d",&x,&y);
        v[x].pb(y);
        v[y].pb(x);
    }
    if(m==n){
        dfsc(1,0);
        //for(auto it:circle)
       // printf("%d ",it);
       // printf("\n");
        if(circle.size()&1){
            a=circle[0],b=circle[1];
            int x=dfs(1,0);
            if(x&1){
                printf("-1\n");
                return 0;
            }
            else{
                val[circle[0]]=-x/2;
                val[circle[1]]=-x/2;
            }
         //   printf("%d %d\n",circle[0],circle[1]);
        //    printf("%d\n",x);
            ans=0;
            dfs(1,0);
            printf("%lld\n",ans+abs(x)/2);
        }
        else{
            a=circle[0],b=circle[1];
            if(dfs(1,0)){
                printf("-1\n");
                return 0;
            }
            int Max=2*n,Min=-2*n;
            while(Max>Min+10){
                int mid1=(Max*2+Min)/3,mid2=(Max+Min*2)/3;
                val[a]=mid1,val[b]=-mid1;
                ans=0;
                dfs(1,0);
                LL a1=ans+abs(mid1);
                val[a]=mid2,val[b]=-mid2;
                ans=0;
                dfs(1,0);
                LL a2=ans+abs(mid2);
                if(a1>a2){
                    Max=mid1;
                }
                else{
                    Min=mid2;
                }
            }
            LL res=1e18;
            for(int i = Min;i<=Max;i++){
                val[a]=i;val[b]=-i;
                ans=0;
                dfs(1,0);
                res=min(ans+abs(i),res);
            }
            printf("%lld\n",res);
            //assert(0);
        }
        return 0;
    }
    if(dfs(1,0)){
        printf("-1\n");
    }
    else
    printf("%lld\n",ans);
}


Measured memory_bytes: 13688