#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
#define N 100050
typedef long long ll;
int head[N],to[N<<1],nxt[N<<1],cnt,n,m,dep[N],vis[N],siz[N],p1,p2,w[N];
int k[N],a[N],la;
inline void add(int u,int v) {
	to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt;
}
void dfs(int x,int y) {
	vis[x]=1; dep[x]=dep[y]+1;
	if(dep[x]&1) siz[x]=1;
	else siz[x]=-1;
	int i;
	for(i=head[x];i;i=nxt[i]) {
		if(to[i]!=y&&!vis[to[i]]) {
			dfs(to[i],x); siz[x]+=siz[to[i]];
		}else if(to[i]!=y) {
			p1=x, p2=to[i];
		}
	}
}
void dfs2(int x,int y) {
	int i; vis[x]=1;
	if(dep[x]&1) siz[x]++;
	else siz[x]--;
	for(i=head[x];i;i=nxt[i]) if(to[i]!=y&&!vis[to[i]]) {
		dfs2(to[i],x); siz[x]+=siz[to[i]]; k[x]+=k[to[i]];
	}
}
int main() {
	scanf("%d%d",&n,&m);
	int i,x,y;
	for(i=1;i<=m;i++) {
		scanf("%d%d",&x,&y); add(x,y); add(y,x);
	}
	dfs(1,0);
	if(m==n-1) {
		if(siz[1]) {puts("-1"); return 0;}
		ll ans=0;
		for(i=1;i<=n;i++) ans+=abs(siz[i]);
		printf("%lld\n",ans);
	}
	else if((dep[p2]-dep[p1]+1)&1) {
		if(abs(siz[1])&1) {puts("-1"); return 0;}
		ll ans=abs(siz[1])/2;int t=siz[1];
		memset(vis,0,sizeof(vis));
		memset(siz,0,sizeof(siz));
		siz[p1]-=t/2, siz[p2]-=t/2; 
		dfs2(1,0);
		for(i=1;i<=n;i++) ans+=abs(siz[i]);
		printf("%lld\n",ans);
	}else {
		if(siz[1]) {puts("-1"); return 0;}
		memset(vis,0,sizeof(vis));
		memset(siz,0,sizeof(siz));
		k[p1]=1, k[p2]=-1;
		dfs2(1,0);
		a[++la]=0;
		ll ans=0;
		for(i=1;i<=n;i++) {
			if(!k[i]) ans+=abs(siz[i]);
			else if(k[i]==1) a[++la]=-siz[i];
			else a[++la]=siz[i];
		}
		sort(a+1,a+la+1);
		int v=a[(la+1)>>1];
		for(i=1;i<=la;i++) ans+=abs(v-a[i]);
		printf("%lld\n",ans);
	}
}























