#include <bits/stdc++.h>
#define MX 200005

using namespace std;

typedef long long ll;

int fst[MX], nxt[MX], v[MX], lnum;
int fa[MX], vis[MX], dep[MX], siz[MX];
int n, m, S, T;

void addeg(int nu, int nv)
{
	nxt[++lnum] = fst[nu];
	fst[nu] = lnum;
	v[lnum] = nv;
}

void input()
{
	int a, b;
	scanf("%d%d", &n, &m);
	for(int i=1; i<=m; i++)
	{
		scanf("%d%d", &a, &b);
		addeg(a, b);
		addeg(b, a);
	}
}

ll ans = 0;

int dfs(int x, int f, int d)
{
	vis[x] = 1;
	dep[x] = d;
	fa[x] = f;
	int s = d;
	for(int i=fst[x]; i; i=nxt[i])
		if(v[i] != f)
		{
			if(!vis[v[i]]) s += dfs(v[i], x, -d);
			else if(!S) S = v[i], T = x;
		}
	if(x != 1) ans += abs(s);
	siz[x] = s;
	return s;
}

void fail()
{
	printf("-1\n");
	exit(0);
}

int main()
{
	input();
	int ret = dfs(1, 0, 1);
	if(abs(ret) & 1) fail();
	else if(ret && !S && !T) fail();
	else if(!ret && !S && !T) printf("%lld\n", ans);
	else
	{
		if(dep[S] == dep[T])
		{
			int p = T;
			ans += abs(ret/2);
			while(p != S) ans -= abs(siz[p]), p = fa[p];
			while(p != 1) ans -= abs(siz[p]), p = fa[p];
			p = T;
			while(p != S) ans += abs(siz[p]-ret/2), p = fa[p];
			while(p != 1) ans += abs(siz[p]-ret), p = fa[p];
			printf("%lld\n", ans);
		}
		else
		{
			if(ret) fail();
			static int tmp[MX], cnt;
			int p = T, x = 0;
			while(p != S) tmp[++cnt] = siz[p], ans -= abs(siz[p]), p = fa[p];
			tmp[++cnt] = 0;
			nth_element(tmp+1, tmp+(cnt+1)/2, tmp+cnt+1);
			x = tmp[(cnt+1)/2];
			p = T;
			ans += abs(x);
			while(p != S) ans += abs(siz[p]-x), p = fa[p];
			printf("%lld\n", ans);
		}
	}
	return 0;
}