#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m,a,b,i,x=1,y,g[N],val[N],su[N],dad[N];
long long ans=0;
int gfa(int x){return g[x]==x?x:g[x]=gfa(g[x]);}
vector<int>e[N],v={0};
void dfs(int x,int fa,int v){su[x]=val[x]=v;dad[x]=fa;for(int y:e[x])if(y!=fa)dfs(y,x,-v),su[x]+=su[y];}
inline void E(){cout<<"-1\n";exit(0);}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>n>>m;for(i=1;i<=n;++i)g[i]=i;
	for(i=0;i<m;++i){
		cin>>a>>b;
		if(gfa(a)==gfa(b))x=a,y=b;else e[a].push_back(b),e[b].push_back(a),g[gfa(a)]=gfa(b);
	}
	dfs(x,0,1);
	if(y && val[y]==1){
		if(su[x]&1)E();
		for(ans=abs(a=su[x]>>1),i=y;i;i=dad[i])su[i]-=a;su[x]=0;
	}else if(su[x])E();
	for(i=1;i<=n;++i)ans+=abs(su[i]);
	if(y && val[y]==-1){
		for(;y!=x;y=dad[y])v.push_back(su[y]),ans-=abs(su[y]);
		nth_element(v.begin(),v.begin()+v.size()/2,v.end());
		a=v[v.size()>>1];for(int u:v)ans+=abs(u-a);
	}
	cout<<ans<<endl;
	return 0;
}