// Slower C++ solution:
#include<cstdio>

#include<cstring>

#include<vector>

#include<algorithm>

using namespace std;

const long long MAXN=100005;



long long N,M;

vector<long long> adj[MAXN];

long long dep[MAXN],sum[MAXN],k[MAXN];

bool vis[MAXN];

long long cU,cV,clen;

vector<long long> num;



void dfs(long long u,long long fa=0)

{

	vis[u]=true;

	for(auto v:adj[u])

	{

		if(v==fa)

			continue;

		if(vis[v])

		{

			cU=u;

			cV=v;

			clen=dep[u]-dep[v]+1;

			continue;

		}

		dep[v]=dep[u]+1;

		dfs(v,u);

	}

}

void Calc(long long u,long long fa=0)

{

	vis[u]=true;

	for(auto v:adj[u])

	{

		if(v==fa||vis[v])

			continue;

		Calc(v,u);

		sum[u]+=sum[v];

		k[u]+=k[v];

	}

}



int main()

{

	scanf("%lld%lld",&N,&M);

	for(long long i=1;i<=M;i++)

	{

		long long a,b;

		scanf("%lld%lld",&a,&b);

		adj[a].push_back(b);

		adj[b].push_back(a);

	}

	dep[1]=0;

	dfs(1);

	long long val=0;

	long long ans=0;

	for(long long i=1;i<=N;i++)

	{

		sum[i]=(dep[i]&1)?1:-1;

		val+=sum[i];

	}

	if(M==N-1)

	{

		if(val!=0)

		{

			puts("-1");

			return 0;

		}

	}

	else

	{

		if(clen&1)

		{

			if(val&1)

			{

				puts("-1");

				return 0;

			}

			else

			{

				ans+=(abs(val)/2);

				sum[cU]-=val/2;

				sum[cV]-=val/2;

			}

		}

		else

		{

			if(val!=0)

			{

				puts("-1");

				return 0;

			}

			else

			{

				k[cU]=1;

				k[cV]=-1;

			}

		}

	}

	memset(vis,0,sizeof vis);

	Calc(1);

	for(long long i=1;i<=N;i++)

	{

		if(k[i]==0)

			ans+=abs(sum[i]);

		else

		{

			if(k[i]==1)

				sum[i]=-sum[i];

			num.push_back(sum[i]);

		}

	}

	num.push_back(0);

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

	long long x=num[(num.size()-1)/2];

	for(auto s:num)

		ans+=1LL*abs(s-x);

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

	

	fclose(stdin);

	fclose(stdout);

	return 0;

}


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

#define int long long

using namespace std;



int n,m,head[100005],nxt[200005],pnt[200005],E = 0,sz[100005],s,t,ans = 0,k[100005];

bool odd = 0;

std::vector<int> v;



void add_edge(int u,int v)

{

	pnt[E] = v;

	nxt[E] = head[u];

	head[u] = E++;

}



void dfs1(int u,int fa)

{

	for (int i = head[u]; i != -1; i = nxt[i])

	{

		int v = pnt[i];

		if (v == fa) continue;

		if (sz[v])

		{

			if (sz[v] == sz[u]) odd = 1;

			s = u;

			t = v;

		}

		else

		{

			sz[v] = -sz[u];

			dfs1(v,u);

		}

	}

}



void dfs2(int u,int fa)

{

	for (int i = head[u]; i != -1; i = nxt[i])

	{

		int v = pnt[i];

		if (v == fa) continue;

		if (v == s && u == t) continue;

		if (v == t && u == s) continue;

		dfs2(v,u);

		sz[u] += sz[v];

		k[u] += k[v];

	}

}



signed main()

{

	memset(head,-1,sizeof(head));

	memset(sz,0,sizeof(sz));

	scanf("%lld%lld",&n,&m);

	for (int i = 1; i <= m; i++)

	{

		int u,v;

		scanf("%lld%lld",&u,&v);

		add_edge(u,v);

		add_edge(v,u);

	}

	sz[1] = 1;

	dfs1(1,0);

	int sum = 0;

	for (int i = 1; i <= n; i++)

	{

		sum += sz[i];

	}

	if (m == n - 1)

	{

		if (sum)

		{

			puts("-1");

			return 0;

		}

	}

	else

	{

		if (odd)

		{

			if (sum & 1)

			{

				puts("-1");

				return 0;

			}

			ans += abs(sum >> 1);

			sz[s] -= sum >> 1;

			sz[t] -= sum >> 1;

		}

		else

		{

			if (sum)

			{

				puts("-1");

				return 0;

			}

			k[s] = 1;

			k[t] = -1;

		}

	}

	dfs2(1,0);

	for (int i = 1; i <= n; i++)

	{

		if (k[i]) v.push_back(k[i] * sz[i]);

		else ans += abs(sz[i]);

	}

	v.push_back(0);

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

	int len = v.size();

	int mid = v[(len + 1) >> 1];

	for (int i = 0; i < len; i++)

	{

		ans += abs(v[i] - mid);	

	}

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

	return 0;

}