#include<bits/stdc++.h>
#define N 200005
#define LL long long
using namespace std;
vector<int> G[N];
int n,m,x,y,c[N],S[N],ufs[N],a[N],la,f[N],cir[N]; LL ans;
int find(int x){ return ufs[x] ? ufs[x]=find(ufs[x]) : x;}
void dfs(int t,int fa)
{
	int i; f[t]=fa;
	c[t]=S[t]=-c[fa];
	for(i=0;i<G[t].size();i++)
		if(G[t][i]!=fa){
			dfs(G[t][i],t);
			S[t]+=S[G[t][i]];
		  }
}
int main()
{
	int i,u,v;
	scanf("%d %d",&n,&m),c[0]=x=1;
	for(i=1;i<=m;i++){
		scanf("%d %d",&u,&v);
		if(find(u)==find(v)){ x=u,y=v; continue;}
		else ufs[find(u)]=find(v);
		G[u].push_back(v);
		G[v].push_back(u);
	  }
	dfs(x,0);
	if(m==n-1){
		if(S[x]){ printf("-1"); return 0;}
		for(i=1;i<=n;i++) ans+=abs(S[i]);
		cout<<ans;
		return 0;
	  }
	for(i=y;i;i=f[i]) cir[i]=1,a[++la]=S[i];
	if(la&1){
		if(S[x]&1){ printf("-1"); return 0;}
		for(i=1;i<=n;i++) if(cir[i]) ans+=abs(S[i]-S[x]/2); else ans+=abs(S[i]);
		cout<<ans;
		return 0;
	  }
	if(S[x]){ printf("-1"); return 0;}
	sort(a+1,a+la+1);
	for(i=1;i<=n;i++) if(cir[i]) ans+=abs(S[i]-a[(la+1)/2]); else ans+=abs(S[i]);
	cout<<ans;
	return 0;
}
