#include <bits/stdc++.h>
#define MP std::make_pair
#define PB push_back
#define FI first
#define SE second
#define M 1000005
int n,m,cx=1,cy,sum[M],S[M],fa[M],Union[M];
std::vector <int> Edge[M];
int read(){
	char c=getchar();int ans=0;
	while (c<'0'||c>'9') c=getchar();
	while (c>='0'&&c<='9') ans=ans*10+c-'0',c=getchar();
	return ans;
}
void Write(int x){
	if (x<10) putchar(x^48);
	else Write(x/10),putchar((x%10)^48);
	return;
}
int find_Union(int x){return Union[x]==x?x:Union[x]=find_Union(Union[x]);}
void DFS(int x,int y){
	sum[x]=y;
	for (auto i:Edge[x])
		if (i!=fa[x]) fa[i]=x,DFS(i,-y),sum[x]+=sum[i];
	return;
}
int main(){
	n=read(),m=read(),std::iota(Union+1,Union+1+n,1);
	for (int i=1,x,y;i<=m;i++){
		x=read(),y=read();
		if (find_Union(x)==find_Union(y)) cx=x,cy=y;
		else Union[find_Union(x)]=find_Union(y),Edge[x].PB(y),Edge[y].PB(x);
	}
	DFS(cx,1);
	if (n==m){
		for (int i=cy;i;i=fa[i]) S[++*S]=sum[i];
		if (S[0]&1){
			if (sum[cx]&1) return !printf("-1");
			for (int i=cy;i;i=fa[i]) sum[i]-=sum[cx]/2;
		}
		else {
			if (sum[cx]) return !printf("-1");
			std::sort(S+1,S+1+*S);
			for (int i=cy;i;i=fa[i]) sum[i]-=S[*S/2];
		}
	}
	else if (sum[cx]) return !printf("-1");
	for (int i=1;i<=n;i++) sum[0]+=(sum[i]>0?sum[i]:-sum[i]);
	return Write(sum[0]),0;
}