#include<bits/stdc++.h>
#define ll long long
#define inf(x) (ll)(1e##x)
using namespace std;
template<typename tn> void read(tn &a){
	tn x=0,f=1; char c=' ';
	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
	for(;isdigit(c);c=getchar()) x=x*10+c-'0';
	a=x*f;
}
const int N = 101000;
int s[N],dep[N],vis[N],fa[N],n,m,p1,p2,tot,a[N],sum,u[N],v[N];
vector<int> e[N];
void dfs(int x,int dad){
	s[x]=-s[dad];sum+=s[x];dep[x]=dep[dad]+1;
	vis[x]=1;fa[x]=dad;
	for(int v:e[x]) if(v!=dad){
		if(s[v]) p1=x,p2=v;
		else dfs(v,x);
	}
}
void dfs2(int x,int dad){
	vis[x]=1;
	for(int v:e[x]) if(!vis[v])
		dfs2(v,x),s[x]+=s[v];
}
void solve1(){
	if(sum){cout<<-1<<'\n';return;}
	dfs2(1,0);
	int ans=0;
	for(int i=1;i<=n;i++) ans+=abs(s[i]);
	cout<<ans<<'\n';
}
void solve2(){
	if(sum&1){cout<<-1<<'\n';return;}
	s[p1]-=sum/2;s[p2]-=sum/2;
	dfs2(1,0);
	int ans=abs(sum)/2;
	for(int i=1;i<=n;i++) ans+=abs(s[i]);
	cout<<ans<<'\n';
}
void solve3(){
	if(sum){cout<<-1<<'\n';return;}
	if(dep[p1]>dep[p2]) swap(p1,p2);
	dfs2(1,0);
	int ans=0;
	while(p1!=p2) a[++tot]=s[p2],vis[p2]=2,p2=fa[p2];
	for(int i=1;i<=n;i++) if(vis[i]<2) ans+=abs(s[i]);
	a[++tot]=0;
	sort(a+1,a+tot+1);
	int k=a[tot+1>>1];
	for(int i=1;i<=tot;i++) ans+=abs(a[i]-k);
	cout<<ans<<'\n';
}
int main(){
	read(n);read(m);
	for(int i=1;i<=m;i++){
		read(u[i]);read(v[i]);
		e[u[i]].push_back(v[i]);
		e[v[i]].push_back(u[i]);
	}
	s[0]=-1;dfs(1,0);
	memset(vis,0,sizeof(vis));
	if(m==n-1){solve1();return 0;}
	if(s[p1]==s[p2]){solve2();return 0;}
	else {solve3();return 0;}
	return 0;
}