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

#include <iostream>

#include <vector>

using namespace std;



const int MAXN = 100000 + 5;



int N, M;

vector<int> Adj[MAXN];

int fa[MAXN], col[MAXN], dep[MAXN];



bool hasCircle;

int circA, circB;



inline void addEdge(int u, int v)

{

	Adj[u].push_back(v);

	Adj[v].push_back(u);

}



void DFS(int cur)

{

	dep[cur] = dep[fa[cur]] + 1;

	col[cur] = (dep[cur] & 1 ? 1 : -1);



	for (int to : Adj[cur])

	{

		if (to == fa[cur])

			continue;

		if (dep[to])

		{

			if (!hasCircle)

				circA = cur, circB = to, hasCircle = true;

		}

		else

			fa[to] = cur, DFS(to), col[cur] += col[to];

	}

}



int solveAcyclic()

{

	if (col[1])

		return -1;



	int ret = 0;

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

		ret += abs(col[i]);

	return ret;

}



int solveOddCircle()

{

	if (col[1] & 1)

		return -1;



	int delta = -col[1] / 2, ret = abs(delta), cur;

	cur = circA;

	while (cur)

		col[cur] += delta, cur = fa[cur];

	cur = circB;

	while (cur)

		col[cur] += delta, cur = fa[cur];



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

		ret += abs(col[i]);

	return ret;

}



int solveEvenCircle()

{

	if (col[1])

		return -1;



	int circle[MAXN] =

	{ 0 }, top = 0;

	for (int cur = circA; cur != circB; cur = fa[cur])

		circle[++top] = col[cur], dep[cur] = 0;



	int mid = (top + 1) >> 1, delta, ret;

	nth_element(&circle[1], &circle[mid], &circle[top + 1]);

	delta = (circle[mid]), ret = abs(delta);



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

		if (dep[i])

			ret += abs(col[i]);

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

		ret += abs(circle[i] - delta);

	return ret;

}



int main()

{

	//printf("%d %d\n", 1 & 1, -1 & 1);

	fflush(stdout);



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

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

	{

		int u, v;

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

		addEdge(u, v);

	}



	DFS(1);



	if (!hasCircle)

		printf("%d\n", solveAcyclic());

	else

	{

		if ((dep[circA] - dep[circB] + 1) & 1)

			printf("%d\n", solveOddCircle());

		else

			printf("%d\n", solveEvenCircle());

	}



	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.357528213735595x):
#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;

}