#include <bits/stdc++.h>
using namespace std;
#define ref(i,x,y)for(int i=x;i<=y;++i)
const int N=100010;
long long ans;
int n,m,x1,x2,d[N],fa[N],s1[N],s2[N],a[N];bool vis[N],tg;
vector<int> e[N];
void dfs(int f,int x){
	fa[x]=f;vis[x]=1;s1[x]=1-d[x];s2[x]=d[x];
	ref(i,0,(int)e[x].size()-1){
		int y=e[x][i];if(y==f)continue;
		if(!vis[y])d[y]=d[x]^1,dfs(x,y),s1[x]+=s1[y],s2[x]+=s2[y];
		else if(!x1)x1=x,x2=y,tg=d[x]==d[y];
	}
}
int main(){
	cin>>n>>m;
	ref(i,1,m){int x,y;cin>>x>>y;e[x].push_back(y);e[y].push_back(x);}
	dfs(0,1);
	if((!x1||x1&&!tg)&&s1[1]!=s2[1]||x1&&n%2)puts("-1");else{
		if(x1&&!tg){
			ref(i,2,n)ans+=abs(s1[i]-s2[i]);
			for(int i=x1;i!=x2;i=fa[i])a[++a[0]]=s1[i]-s2[i];
			a[++a[0]]=s1[1]-s2[1];
			sort(a+1,a+a[0]+1);int x=a[a[0]/2+1];
			ref(i,1,a[0])ans-=abs(a[i]),ans+=abs(a[i]-x);
		}else{
			if(tg){
				ans=abs(n/2-s2[1]);
				int t=n/2-s2[1];
				for(int i=x1;i!=x2;i=fa[i])s2[i]+=t;
				for(int i=x2;i;i=fa[i])s2[i]+=t*2;
			}
			ref(i,2,n)ans+=abs(s1[i]-s2[i]);
		}
		cout<<ans<<endl;
	}
}