#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#define N (200000+1000)
using namespace std;

struct Edge{int to,next;}edge[N<<1];
int n,m,u,v,K[N],size[N],sum,cir_num;
int Depth[N],Father[N],stx,sty;
int head[N],num_edge,Q[N],tot;

void add(int u,int v)
{
	edge[++num_edge].to=v;
	edge[num_edge].next=head[u];
	head[u]=num_edge;
}

void Dfs(int x,int fa)
{
	Depth[x]=Depth[fa]+1;
	Father[x]=fa;
	size[x]=(Depth[x]%2==1)?1:-1;
	for (int i=head[x]; i; i=edge[i].next)
		if (edge[i].to!=fa)
		{
			if (Depth[edge[i].to])
			{
				if (cir_num) continue;
				stx=x; sty=edge[i].to;
				cir_num=Depth[x]-Depth[edge[i].to]+1;
				continue;
			}
			Dfs(edge[i].to,x);
			size[x]+=size[edge[i].to];
		}
}

int LCA(int x,int y)
{
	while (Depth[x]>Depth[y]) x=Father[x];
	while (Depth[y]>Depth[x]) y=Father[y];
	while (x!=y) x=Father[x], y=Father[y];
	return x;
}

int main()
{
//	freopen("data.in","r",stdin);
//	freopen("datam.out","w",stdout);
	scanf("%d%d",&n,&m);
	for (int i=1; i<=m; ++i)
	{
		scanf("%d%d",&u,&v);
		add(u,v); add(v,u);
	}
	Dfs(1,0);

	if (m==n-1)//tree
	{
		if (size[1]!=0) printf("-1");
		else
		{
			long long ans=0;
			for (int i=1; i<=n; ++i) ans+=abs(size[i]);
			printf("%lld",ans);	
		}
	}
	else if (cir_num%2==1)//odd
	{
		if (abs(size[1])%2==1) printf("-1");
		else
		{
			int sz=size[1];
			long long ans=abs(sz)/2;
			for (int u=stx; u; u=Father[u]) size[u]-=sz/2;
			for (int u=sty; u; u=Father[u]) size[u]-=sz/2;
			for (int i=1; i<=n; ++i) ans+=abs(size[i]);
			printf("%lld",ans);	
		}
	}
	else//even
	{
		if (size[1]!=0) printf("-1");
		else
		{
			int lca=LCA(stx,sty);
			for (int u=stx; u!=lca; u=Father[u])
				Q[++tot]=size[u],Depth[u]=0;
			for (int u=sty; u!=lca; u=Father[u])
				Q[++tot]=-size[u],Depth[u]=0;
			Q[++tot]=0; 
			sort(Q+1,Q+tot+1);
			int sz=Q[tot/2];
			
			long long ans=0;
			for (int i=1; i<=n; ++i) if (Depth[i]) ans+=abs(size[i]);
			for (int i=1; i<=tot; ++i) ans+=abs(Q[i]-sz);
			printf("%lld",ans);
		}
	}
}