#include <algorithm>
#include <cstdio>

const int N=100005;

int fst[N],v[N],k[N],a[N],ans=0,tot=0,A,B;
bool u[N],typ[N];

struct Edge{
	int to,nxt;
}e[N*2];

void addedge(int x,int y){
	e[++tot].to=y;e[tot].nxt=fst[x];fst[x]=tot;
}

void dfs(int x,int father){
	u[x]=1;
	for (int i=fst[x];i;i=e[i].nxt){
		int y=e[i].to;
		if (y!=father){
			if (u[y]){
				A=x;B=y;continue;
			}
			typ[y]=typ[x]^1;
			dfs(y,x);
		}
	}
}

void solve(int x){
	u[x]=0;
	for (int i=fst[x];i;i=e[i].nxt){
		int y=e[i].to;
		if (u[y]){
			solve(y);
			v[x]+=v[y];
			k[x]+=k[y];
		}
	}
	if (!k[x]) ans+=abs(v[x]);
}
int main(){
	int n,m;
	scanf("%d%d\n",&n,&m);
	for (int i=1;i<=m;i++){
		int x,y;
		scanf("%d%d\n",&x,&y);
		addedge(x,y);
		addedge(y,x);
	}
	dfs(1,0);
	int cnt=0;
	for (int i=1;i<=n;i++) cnt+=(v[i]=typ[i]?1:-1);
	if (n-1==m){
		if (cnt) ans=-1;
			else solve(1);
	}
	else if (typ[A]==typ[B]){
		if (cnt&1) ans=-1;
		else{
			v[A]-=cnt/2;v[B]-=cnt/2;
			ans=abs(cnt/2);
			solve(1);
		}
	}
	else if (cnt) ans=-1;
	else{
		k[A]=1;k[B]=-1;
		solve(1);
		int len=1;a[1]=0;
		for (int i=1;i<=n;i++)
			if (k[i]==1) a[++len]=-v[i];
				else if (k[i]==-1) a[++len]=v[i];
		std::sort(a+1,a+len+1);
		for (int i=1;i<=len;i++)
			ans+=abs(a[i]-a[len/2+1]);
	}
	printf("%d\n",ans);
}