#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long ll;

struct Edge {
  int s,t,next;
  Edge() {}
  Edge(int a,int b,int c):s(a),t(b),next(c) {}
};

Edge e[200005];
int head[100005];

bool vis[100005];
int id;

void dfs1(int x,int fa) {
  vis[x]=1;
  for(int i=head[x];i;i=e[i].next)
    if (e[i].t!=fa) {
    	int u=e[i].t;
    	if (!vis[u]) dfs1(u,x);
    	else id=i; 
	}
}

ll num[100005];
int fa[100005];
bool dep[100005];

void dfs2(int x) {
  num[x]=1;
  for(int i=head[x];i;i=e[i].next)
    if (e[i].t!=fa[x]&&((i+1)>>1)!=((id+1)>>1)) {
    	int u=e[i].t;
    	fa[u]=x;dep[u]=dep[x]^1;
    	dfs2(u);
    	num[x]-=num[u];
	}
}

ll now[100005];

int main() {
  int n,m;
  scanf("%d%d",&n,&m);
  for(int i=1;i<=m;i++) {
  	int x,y;
  	scanf("%d%d",&x,&y);
  	e[2*i-1]=Edge(x,y,head[x]);
  	head[x]=2*i-1;
  	e[2*i]=Edge(y,x,head[y]);
  	head[y]=2*i;
  }
  dfs1(1,0);
  if (!id) {
  	dfs2(1);
  	if (num[1]!=0) puts("-1");
  	else {
  		ll ans=0;
  		for(int i=1;i<=n;i++) ans+=abs(num[i]);
  		printf("%lld\n",ans);
	  }
  }
  else {
  	int p=e[id].s,q=e[id].t;
  	dfs2(p);
  	if (dep[p]^dep[q]) {
  		if (num[p]!=0) puts("-1");
  		else {
  			int cnt=0;
  			for(int i=q;i!=p;i=fa[i]) 
			  now[++cnt]=(dep[i]^dep[q])?-num[i]:num[i];
			now[++cnt]=0;
  			nth_element(now+1,now+((cnt+1)>>1),now+cnt+1);
  			ll t=now[(cnt+1)>>1];
  			for(int i=q;i!=p;i=fa[i]) 
  			  if (dep[i]^dep[q]) num[i]+=t; else num[i]-=t;
  			ll ans=abs(t);
  			for(int i=1;i<=n;i++) ans+=abs(num[i]);
  			printf("%lld\n",ans);
		  }
	  }
	else {
		if (num[p]&1) puts("-1");
		else {
			ll t=(abs(num[p])>>1);
			if (num[p]>0) {
			  for(int i=q;i!=p;i=fa[i])
			    if (dep[i]^dep[q]) num[i]+=t; else num[i]-=t;
		    }
		    else {
			  for(int i=q;i!=p;i=fa[i])
			    if (dep[i]^dep[q]) num[i]-=t; else num[i]+=t;
			}
			num[p]=0;
			ll ans=t;
			for(int i=1;i<=n;i++) ans+=abs(num[i]);
			printf("%lld\n",ans); 
		}
	}
  }
  return 0;
}