#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+7;
int n,m,s,t;
int f[N],dep[N],sum[N];
vector<int> ve[N];
void dfs(int x){
	dep[x]=dep[f[x]]+1; sum[x]=dep[x]&1?1:-1;
	for(auto to:ve[x]) if(to!=f[x]) f[to]=x,dfs(to),sum[x]+=sum[to];
}
inline int find(int x){
	return f[x]==x?x:f[x]=find(f[x]);
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i) f[i]=i;
	for(int i=1,a,b;i<=m;++i){
		scanf("%d%d",&a,&b);
		if(find(a)==find(b)) s=a,t=b;
		else f[find(a)]=find(b),ve[a].push_back(b),ve[b].push_back(a);
	}
	memset(f,0,sizeof(f)); dfs(1);
	if(!s){
		if(sum[1]) return puts("-1"),0;
		ll ans=0;
		for(int i=1;i<=n;++i) ans+=abs(sum[i]);
		printf("%lld\n",ans);
	}
	else{
		if(dep[s]+dep[t]&1){
			if(sum[1]) return puts("-1"),0;
			vector<int> vec(1,0); ll ans=0; int x=s,y=t;
			while(x^y){
				if(dep[x]>dep[y]) vec.emplace_back(sum[x]),x=f[x];
				else vec.emplace_back(-sum[y]),y=f[y];
			}
			int mid=vec.size()>>1;
			nth_element(vec.begin(),vec.begin()+mid,vec.end());
			for(x=s;x;x=f[x]) sum[x]-=vec[mid];
			for(y=t;y;y=f[y]) sum[y]+=vec[mid];
			for(int i=1;i<=n;++i) ans+=abs(sum[i]);
			printf("%lld\n",ans+abs(vec[mid]));
		}
		else{
			if(sum[1]&1) return puts("-1"),0;
			int det=-sum[1]/2; ll ans=0;
			for(int x=s;x;x=f[x]) sum[x]+=det;
			for(int x=t;x;x=f[x]) sum[x]+=det;
			for(int i=1;i<=n;++i) ans+=abs(sum[i]);
			printf("%lld\n",ans+abs(det));
		}
	}
	return 0;
}