#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=2e5;
int head[N],nxt[N],to[N],st[N];
int fa[N];inline int find(int x) { return fa[x]==x?x:fa[x]=find(fa[x]); }
LL ans,sum[N],siz[N];
bool vis[N];
inline int dfs(int k) {
	int t;
	for (int i=head[k];i;i=nxt[i])
		if (!vis[to[i]]) {
			vis[to[i]]=true;
			t=dfs(to[i]);
			ans+=abs(t);
			siz[k]-=t;
		}
	return siz[k];
}
inline void get(int k) {
	for (int i=head[k];i;i=nxt[i])
		if (to[i]!=fa[k])
			fa[to[i]]=k,get(to[i]);
}
int main()
{
	int n,m,i,tot=0,a,b,A,B,top=0,t,k;cin>>n>>m;
	for (i=1;i<=n;i++) fa[i]=i,siz[i]=1;
	for (i=1;i<=m;i++) {
		cin>>a>>b;
		if (find(a)==find(b)) A=a,B=b;
		else {
			fa[fa[a]]=b;
			to[++tot]=b,nxt[tot]=head[a],head[a]=tot;
			to[++tot]=a,nxt[tot]=head[b],head[b]=tot;
		}
	}
	if (m==n-1)
		vis[1]=true,printf("%lld\n",dfs(1)?-1:ans);
	else {
		for (i=1;i<=n;i++) fa[i]=0;
		get(A);
		for (k=B;k;k=fa[k]) st[++top]=k;
		if (top&1) {
			vis[A]=true;t=dfs(A);
			for (i=1;i<=n;i++) vis[i]=false,siz[i]=1;
			if (t%2) return puts("-1"),0;
			siz[A]-=t/2,siz[B]-=t/2;
			ans=abs(t/2);
			vis[A]=true;dfs(A);
		} else {
			for (i=1;i<=top;i++) vis[st[i]]=true;
			for (i=1;i<=top;i++) sum[i]=sum[i-1]+(i&1?-1:1)*dfs(st[i]);
			if (sum[top]) return puts("-1"),0;
			sort(sum+1,sum+1+top);
			for (i=1;i<=top;i++) ans+=abs(sum[top/2]-sum[i]);
		}
		printf("%lld\n",ans);
	}
	return 0;
}