#include<bits/stdc++.h>
#define rep(i,x,y) for (int i=(x); i<=(y); i++)
#define ll long long
#define N 100010
#define inf 1000000000
using namespace std;
ll read(){
	char ch=getchar(); ll x=0; int op=1;
	for (; !isdigit(ch); ch=getchar()) if (ch=='-') op=-1;
	for (; isdigit(ch); ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
	return x*op;
}
int n,m,cnt,tot,head[N],dep[N],s[N],k[N],a[N],sum,ans,len,sx,sy; bool vis[N];
struct edge{ int to,nxt; }e[N<<1];
void adde(int x,int y){
	e[++cnt].to=y; e[cnt].nxt=head[x]; head[x]=cnt;
}
void ins(int x,int y){
	adde(x,y); adde(y,x);
}
void dfs(int u,int fa){
	vis[u]=1; if (dep[u]&1) sum--; else sum++;
	for (int i=head[u],v; i; i=e[i].nxt) if ((v=e[i].to)!=fa){
		if (!vis[v]) dep[v]=dep[u]+1,dfs(v,u);
		else if (dep[v]<dep[u]) len=dep[u]-dep[v]+1,sx=u,sy=v;
	}
}
void dfs2(int u,int fa){
	vis[u]=1;
	for (int i=head[u],v; i; i=e[i].nxt) if (!vis[v=e[i].to]){
		dfs2(v,u); s[u]+=s[v]; k[u]+=k[v];
	}
}
int main(){
	n=read(); m=read();
	rep (i,1,m){
		int x=read(),y=read();
		ins(x,y);
	}
	dfs(1,0);
	if ((len&1) && (abs(sum)&1) || !(len&1) && sum){ puts("-1"); return 0; }
	rep (i,1,n) if (dep[i]&1) s[i]=1; else s[i]=-1;
	if (len&1) s[sx]+=sum/2,s[sy]+=sum/2,ans+=abs(sum/2);
	else k[sx]=1,k[sy]=-1;
	memset(vis,0,sizeof(vis));
	dfs2(1,0);
	rep (i,1,n) if (k[i]) a[++tot]=-k[i]*s[i];
	sort(a+1,a+1+tot);
	int X=a[(1+tot)/2]; ans+=abs(X);
	rep (i,1,n) ans+=abs(k[i]*X+s[i]);
	printf("%d\n",ans);
	return 0;
}