// Slower C++ solution:
#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;

} 

// Faster C++ solution for the same problem (measured speedup: 1.4049303360706917x):
#include<bits/stdc++.h>

using namespace std;

#define nn 100008

#define ll long long

vector<int> v[nn];

vector<int> cir;int pa[nn];

int siz[nn];int dep[nn];

int n,m;ll ans;

int c0,c1;



int vis[nn];int tag[nn];

void dfs(int x,int p)

{

	pa[x]=p;vis[x]=1;

	for(int i=0;i<v[x].size();i++)

	{

		int y=v[x][i];

		if(y==p) continue;

		if(!vis[y]) dfs(y,x);

		else if(!cir.size())

		{

			int xx=x;

			while(xx!=y) cir.push_back(xx),xx=pa[xx];

			cir.push_back(y);

		}

	}

}

void efs(int x)

{

	siz[x]=tag[x];if(tag[x]<0) c0++;else c1++;

	for(int i=0;i<v[x].size();i++)

	{

		int y=v[x][i];

		if(!tag[y]) 

			tag[y]=-tag[x],efs(y),siz[x]+=siz[y];

	}

}







void makeit(int x)

{

	for(int i=0;i<cir.size();i++)

	{

		ans+=abs(x);

		x=siz[cir[i]]-x;x=-x;

	}

	printf("%lld\n",ans);		

}

int main()

{

	tag[0]=1;

	#define err return 0*puts("-1");

	#define output return 0*printf("%lld\n",ans);

	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);

	}

	

	if(m<n)

	{

		tag[1]=1;efs(1);

		if(c0!=c1) err;

		for(int i=1;i<=n;i++) ans+=abs(siz[i]);

		output;

	}

	else

	{

		dfs(1,0);memset(vis,0,sizeof vis);

		int tot=0;

			

		for(int i=0;i<cir.size();i++) tag[cir[i]]=(i%2==0)*2-1;

		for(int i=0;i<cir.size();i++) efs(cir[i]),tot+=siz[cir[i]],ans-=abs(siz[cir[i]]);

		for(int i=1;i<=n;i++) ans+=abs(siz[i]);

			

		if(cir.size()%2==1)

		{

			if(tot%2) err;

			

			makeit(tot/2);

		}

		else

		{

			if(tot) err;

			

			int x=0,y=-1;vector<int> all;

			for(int i=0;i<cir.size();i++)

			{

				all.push_back(x*y);

			//	y=-y;

				x=siz[cir[i]]-x;

				x=-x;

			}

			

			sort(all.begin(),all.end());

			makeit(all[all.size()/2]);	

		}

	}

	

	

	return 0;

}