#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m,f[100100],val[100100],s[100100],a,b,ff[100100],d[100100],cnt;
vector<int>v[100100];
inline int sf(int x){return f[x]==x?x:f[x]=sf(f[x]);}
inline void dfs(int x,int fa,int wh){
	s[x]=val[x]=wh;ff[x]=fa;
	for(int i=0;i<v[x].size();i++)
	  if(v[x][i]!=fa)dfs(v[x][i],x,-wh),s[x]+=s[v[x][i]];  
}
signed main(){
    int i,j,k,ans=0;
    scanf("%lld%lld",&n,&m);
    for(i=1;i<=n;i++)f[i]=i;
    for(i=1;i<=m;i++){
      int x,y;
      scanf("%lld%lld",&x,&y);
      if(sf(x)==sf(y))a=x,b=y;
        else f[sf(x)]=sf(y),v[x].push_back(y),v[y].push_back(x);
    }
    if(!a)a=1;dfs(a,0,1);
    if(!b){
      if(s[a]){puts("-1");return 0;}
      for(i=1;i<=n;i++)ans+=abs(s[i]);
      printf("%lld\n",ans);return 0;
    }
    if(val[a]==val[b]){
      if(s[a]%2!=0){puts("-1");return 0;}
      k=s[a]/2;for(i=b;i;i=ff[i])s[i]-=k;ans=abs(k);
      s[a]=0;for(i=1;i<=n;i++)ans+=abs(s[i]);
      printf("%lld\n",ans);return 0;
    }
    if(s[a]){puts("-1");return 0;}
    for(i=1;i<=n;i++)ans+=abs(s[i]);
    d[++cnt]=0;
    for(i=b;i!=a;i=ff[i])d[++cnt]=s[i],ans-=abs(s[i]);
    sort(d+1,d+cnt+1);
    for(i=1;i<=cnt;i++)ans+=abs(d[i]-d[(cnt+1)/2]);
    printf("%lld\n",ans);return 0;
}
