#include<bits/stdc++.h>
using namespace std;
#define nn 400008
#define ll long long

vector<int> v[nn];
int n,m;int siz[nn];
int tag[nn];ll ans=0;
int sx,sy;

int c0,c1;
void dfs(int x,int p)
{
	tag[x]=-tag[p];
	for(int i=0;i<v[x].size();i++)
	{
		if(v[x][i]!=p and !tag[v[x][i]])
			dfs(v[x][i],x);
		else if(v[x][i]!=p)
			sx=x,sy=v[x][i];
	}
	if(tag[x]<0) c0++;else c1++;
}

int tp[nn];
vector<int> all;int vis[nn];
void solve(int x,int p)
{
	siz[x]=tag[x];vis[x]=1;

	for(int i=0;i<v[x].size();i++)
	{
		if(v[x][i]!=p and !vis[v[x][i]])
		{
			solve(v[x][i],x);
			siz[x]+=siz[v[x][i]],tp[x]+=tp[v[x][i]];
		}
	}

	if(!tp[x]) ans+=abs(siz[x]);
	else if(tp[x]>0) all.push_back(-siz[x]);
	else all.push_back(siz[x]);
}

void sol()
{
	solve(1,0);
	all.push_back(0);sort(all.begin(),all.end());
	int x=all[all.size()/2];
	for(int i=0;i<all.size();i++) 
		ans+=abs(x-all[i]);
		
	printf("%lld\n",ans);
}

#define err {puts("-1");exit(0);}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		v[x].push_back(y);v[y].push_back(x);
	}
	tag[0]=1;dfs(1,0);
	if(m==n-1)
	{
		if(c0!=c1) err;sol();
	}
	else
	{
		if(tag[sx]==tag[sy])
		{
			if((c0-c1)%2) err;
			ans=abs(c0-c1)/2;
			tag[sx]+=(c0-c1)/2,tag[sy]+=(c0-c1)/2;
			sol();
		}
		else
		{
			if(c0!=c1) err;
			tp[sx]++,tp[sy]--;
			sol();
		}
	}
	return 0;
}