#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
inline long long myabs(long long x){return x<0?-x:x;}
int n,m;
vector<int>G[100010];
int dep[100010];
long long b[100010];
long long a[100010];
int par[100010];
int tot;
long long ans;
int u,v;
void prework(int x,int p)
{
	par[x]=p;
	b[x]=(dep[x]&1)?1:-1;
	for(int i=0;i<(int)G[x].size();i++)
	{
		int y=G[x][i];
		if(y==p)continue;
		if(dep[y])
		{
			if(dep[y]<dep[x])u=x,v=y;
			continue;
		}
		dep[y]=dep[x]+1;
		prework(y,x);
		b[x]+=b[y];
	}
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		G[x].push_back(y);
		G[y].push_back(x);
	}
	dep[1]=1;
	prework(1,0);
	if(n-1==m)
	{
		if(b[1]!=0)
		{
			printf("-1");
			return 0;
		}
		for(int i=1;i<=n;i++)ans+=myabs(b[i]);
	}
	else
	{
		if((dep[u]-dep[v])%2==0)
		{
			if(b[1]%2!=0)
			{
				printf("-1");
				return 0;
			}
			long long t=b[1]/2;
			while(u)b[u]-=t,u=par[u];
			while(v)b[v]-=t,v=par[v];
			ans=myabs(t);
			for(int i=1;i<=n;i++)ans+=myabs(b[i]);
		}
		else
		{
			if(b[1]!=0)
			{
				printf("-1");
				return 0;
			}
			while(u!=v)
			{
				a[++tot]=b[u];
				dep[u]=0;
				u=par[u];
			}
			sort(a+1,a+tot+1);
			long long x=a[(tot+1)/2];
			ans=myabs(x);
			for(int i=1;i<=tot;i++)ans+=myabs(a[i]-x);
			for(int i=1;i<=n;i++)if(dep[i])ans+=myabs(b[i]);
		}
	}
	printf("%lld",ans);
	return 0;
}