#include <bits/stdc++.h>
#define N 100005
#define ll long long
#define NoSolution return puts("-1"),0 
#define For(i,x,y) for(int i=(x);i<=(y);++i)
#define Rof(i,x,y) for(int i=(x);i>=(y);--i)
#define Edge(x) for(int i=head[x];i;i=e[i].nxt)
#define mset(x,y) memset(x,y,sizeof(x))
#define p_b push_back 
using namespace std;

int f[N],fa[N],dep[N],cnt=0,b[N],_u,_v;
vector<int> g[N];

void dfs(int x,int v){
	f[x]=v;
	for(auto to:g[x]){
		if(!dep[to]){
			fa[to]=x,dep[to]=dep[x]+1;
			dfs(to,-v);
			f[x]+=f[to];
		} else if(to!=fa[x]){
			_u=x,_v=to;
		}
	}
}
int main(){
	int n,m,u,v,ans=INT_MAX,mid,tmp,cnt;
	dep[1]=1;
	scanf("%d%d",&n,&m);
	For(i,1,m){
		scanf("%d%d",&u,&v);
		g[u].p_b(v),g[v].p_b(u);
	}
	dfs(1,1);
	if(m==n-1){
		ans=0;
		if(f[1]) NoSolution;
		For(i,1,n) ans+=abs(f[i]);
		printf("%d\n",ans);
	} else{
		if((dep[_v]-dep[_u]+1)%2==0){
			if(f[1]) NoSolution;
			tmp=0,cnt=0;
			For(i,1,n) tmp+=abs(f[i]);
			ans=min(ans,tmp);
			for(int i=_v;i!=_u;i=fa[i]) tmp-=abs(f[i]),b[++cnt]=f[i];
			sort(b+1,b+cnt+1);
			cnt&1?mid=b[(cnt+1)/2]:mid=(b[cnt/2]+b[cnt/2+1])/2;
			tmp+=abs(mid);
			for(int i=_v;i!=_u;i=fa[i]) tmp+=abs(f[i]-mid);
			ans=min(ans,tmp);
			printf("%d\n",ans);		
		} else{
			if(f[1]%2) NoSolution;
			int tmp=f[1]/2;ans=abs(tmp);
			for(int i=_u;i;i=fa[i]) f[i]-=tmp;
			for(int i=_v;i;i=fa[i]) f[i]-=tmp;
			For(i,1,n) ans+=abs(f[i]);
			printf("%d\n",ans);
		}
	}
}