#include <cstdio>
#include <vector>
#include <algorithm>
#include <numeric>
int N, M, head[100001], next[200001], to[200001], E, q[100001], fa[100001], dep[100001], fe[100001], v[100001], size[100001][2], cir[100001], L, oncir[100001];
long long S[100001];
inline void add(int u, int v)
{
	next[++E] = head[u], to[E] = v, head[u] = E;
	next[++E] = head[v], to[E] = u, head[v] = E;
}
void Tree()
{
	int H = 0, T = 1, u;
	q[1] = 1;
	while (H < T)
		for (int e = head[u = q[++H]]; e; e = next[e])
			if (to[e] != fa[u])
			{
				fa[q[++T] = to[e]] = u;
				dep[to[e]] = dep[u] + 1;
			}
	for (int i = N; i; i--)
	{
		int u = q[i];
		size[u][dep[u] & 1] = 1;
		for (int e = head[u]; e; e = next[e])
			if (to[e] != fa[u])
			{
				size[u][0] += size[to[e]][0];
				size[u][1] += size[to[e]][1];
			}
	}
	if (size[1][0] != size[1][1])
		puts("-1");
	else
	{
		long long O = 0;
		for (int i = 2; i <= N; i++)
			O += std::max(size[i][0] - size[i][1], size[i][1] - size[i][0]);
		printf("%lld\n", O);
	}
}
void Circle()
{
	int H = 0, T = 1, u, MU = 0, MV = 0;
	q[1] = 1;
	v[1] = 1;
	while (H < T)
		for (int e = head[u = q[++H]]; e; e = next[e])
			if (e != ((fe[u] + 1) ^ 1) - 1)
			{
				if (v[to[e]])
				{
					if (e & 1)
					{
						MU = u;
						MV = to[e];
					}
				}
				else
				{
					fa[q[++T] = to[e]] = u;
					fe[to[e]] = e;
					dep[to[e]] = dep[u] + 1;
					v[to[e]] = 1;
				}
			}
	static int LH[100001], RH[100001], LN, RN;
	while (dep[MU] > dep[MV])
	{
		LH[++LN] = MU;
		MU = fa[MU];
	}
	while (dep[MU] < dep[MV])
	{
		RH[++RN] = MV;
		MV = fa[MV];
	}
	while (MU != MV)
	{
		LH[++LN] = MU;
		MU = fa[MU];
		RH[++RN] = MV;
		MV = fa[MV];
	}
	LH[++LN] = MU;
	std::reverse(RH + 1, RH + RN + 1);
	std::copy(LH + 1, LH + LN + 1, cir + 1);
	std::copy(RH + 1, RH + RN + 1, cir + LN + 1);
	L = LN + RN;
	for (int i = 1; i <= L; i++)
		oncir[cir[i]] = i;
	long long O = 0;
	for (int i = 1; i <= L; i++)
	{
		H = 0, T = 1;
		q[1] = cir[i];
		fa[cir[i]] = 0;
		dep[cir[i]] = 0;
		while (H < T)
			for (int e = head[u = q[++H]]; e; e = next[e])
				if (!oncir[to[e]] && to[e] != fa[u])
				{
					fa[q[++T] = to[e]] = u;
					dep[to[e]] = dep[u] + 1;
				}
		for (int i = T; i; i--)
		{
			int u = q[i];
			size[u][dep[u] & 1] = 1;
			for (int e = head[u]; e; e = next[e])
				if (!oncir[to[e]] && to[e] != fa[u])
				{
					size[u][0] += size[to[e]][0];
					size[u][1] += size[to[e]][1];
				}
		}
		for (int i = 2; i <= T; i++)
			O += std::max(size[q[i]][0] - size[q[i]][1], size[q[i]][1] - size[q[i]][0]);
	}
	for (int i = 1; i <= L; i++)
	{
		S[i] = size[cir[i]][0] - size[cir[i]][1];
		if (i & 1)
			S[i] = -S[i];
	}
	if (L & 1)
	{
		long long D = std::accumulate(S + 1, S + L + 1, 0) >> 1;
		O += std::max(D, -D);
		S[1] -= D;
		S[L] -= D;
		D = 0;
		for (int i = 1; i < L; i++)
		{
			D += S[i];
			O += std::max(D, -D);
		}
	}
	else
	{
		std::vector < long long > Z;
		long long D = 0;
		for (int i = 1; i <= L; i++)
			Z.push_back(D += S[i]);
		if (D)
		{
			puts("-1");
			return;
		}
		std::nth_element(Z.begin(), Z.begin() + (Z.size() >> 1), Z.end());
		long long Mid = Z[Z.size() >> 1];
		for (long long i : Z)
			O += std::max(Mid - i, i - Mid);
	}
	printf("%lld\n", O);
}
int main()
{
	scanf("%d%d", &N, &M);
	for (int i = 1, u, v; i <= M; i++)
	{
		scanf("%d%d", &u, &v);
		add(u, v);
	}
	if (N & 1)
	{
		puts("-1");
		return 0;
	}
	if (M == N - 1)
		Tree();
	else
		Circle();
	return 0;
}


Measured memory_bytes: 8056