#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define nn 65536*2
#define lf double
int n,m;
vector<int> v[nn];
int dep[nn];int siz[nn];
ll ans=0;int cnt[2];int vis[nn];
int adv[nn];
void dfs(int x,int p)
{
	vis[x]=1;
	dep[x]=dep[p]^1;cnt[dep[x]]++;
	if(dep[x]==0) siz[x]=-1-adv[x];else siz[x]=1+adv[x];
	//siz[x]+=adv[x];
	for(int i=0;i<v[x].size();i++)
	{
		if(vis[v[x][i]]) continue;
		dfs(v[x][i],x);siz[x]+=siz[v[x][i]];
	}
	ans+=abs(siz[x]);
}

int ctrl;int ij,ji;
void efs(int x,int p)
{
	dep[x]=dep[p]^1;vis[x]=1;
	for(int i=0;i<v[x].size();i++)
	{
		if(v[x][i]==p) continue;
		if(vis[v[x][i]]) 
		{
			if(dep[v[x][i]]==dep[x]) 
				ctrl=1,ij=x,ji=v[x][i];
			else
				ctrl=0,ij=x,ji=v[x][i];
		}
		else efs(v[x][i],x);
	}
}

vector<int> lft;
void spj(int x,int p)
{
	vis[x]=1;
	dep[x]=dep[p]^1;cnt[dep[x]]++;
	if(dep[x]==0) siz[x]=-1;else siz[x]=1;
	for(int i=0;i<v[x].size();i++)
	{
		if(vis[v[x][i]]) continue;
		spj(v[x][i],x);
		adv[x]+=adv[v[x][i]];
		siz[x]+=siz[v[x][i]];
	}
	if(adv[x]==1) lft.push_back(-siz[x]);
	else if(adv[x]==-1) lft.push_back(siz[x]);
	else ans+=abs(siz[x]);
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++)
	{
		int x,y;scanf("%d%d",&x,&y);
		v[x].push_back(y);v[y].push_back(x);
	}
	if(m==n-1)
	{
		dfs(1,0);
		if(cnt[0]!=cnt[1]) puts("-1");
		else printf("%lld\n",ans);
		return 0;
	}
	efs(1,0);
	if(ctrl)
	{
		memset(vis,0,sizeof(vis));
		dfs(1,0);int o=(cnt[!dep[ij]]-cnt[dep[ij]]);//assert(dep[ij]==dep[ji]);
		if(o%2)
		{
			puts("-1");
			return 0;
		}
		adv[ij]+=o/2,adv[ji]+=o/2;//cout<<o<<":"<<o/2<<"\n";
		ans=0;memset(vis,0,sizeof(vis));dfs(1,0);
		printf("%lld\n",ans+abs(o)/2);
		return 0;
	}
	else
	{
	//	exit(0);
		memset(vis,0,sizeof(vis));adv[ij]++,adv[ji]--;
		spj(1,0);lft.push_back(0);sort(lft.begin(),lft.end());
		
		for(int i=0;i<lft.size();i++) ans+=abs(lft[i]-lft[lft.size()/2]);
		if(cnt[0]!=cnt[1]) puts("-1");
		else printf("%lld\n",ans);
		return 0;
	}
	return 0;
} 