#include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define mset(x, y) memset(x, y, sizeof x)
#define mcpy(x, y) memcpy(x, y, sizeof x)
using namespace std;

typedef long long LL;
typedef pair <int, int> pii;

inline int Read()
{
	int x = 0, f = 1, c = getchar();
	for (; !isdigit(c); c = getchar())
		if (c == '-')
			f = -1;
	for (;  isdigit(c); c = getchar())
		x = x * 10 + c - '0';
	return x * f;
}

const int MAXN = 100005;

int n, m, rta = 1, rtb, f[MAXN], siz[MAXN], sic[MAXN];
bool cir[MAXN], col[MAXN];
vector <int> adj[MAXN]; 
LL ans;

inline int Find(int x)
{
	while (x ^ f[x])
		x = f[x] = f[f[x]];
	return x;
}

inline void Dfs(int x, int p)
{
	sic[x] = col[x], siz[x] = 1, cir[x] = x == rtb;
	for (auto y : adj[x])
		if (y ^ p)
			col[y] = col[x] ^ 1, Dfs(y, x), siz[x] += siz[y], sic[x] += sic[y], cir[x] |= cir[y];
}

int main()
{
#ifdef wxh010910
	freopen("data.in", "r", stdin);
#endif
	n = Read(), m = Read();
	if (n & 1)
		return puts("-1"), 0;
	for (int i = 1; i <= n; i ++)
		f[i] = i;
	for (int i = 1, x, y; i <= m; i ++)
	{
		x = Read(), y = Read();
		if (Find(x) ^ Find(y))
			f[Find(x)] = Find(y), adj[x].pb(y), adj[y].pb(x);
		else
			rta = x, rtb = y;
	}
	Dfs(rta, 0);
	if (m == n - 1)
	{
		if (sic[1] << 1 ^ n)
			return puts("-1"), 0;
		for (int i = 1; i <= n; i ++)
			ans += abs(siz[i] - sic[i] - sic[i]);
		return printf("%lld\n", ans), 0;
	}
	if (col[rtb])
	{
		if (sic[rta] << 1 ^ n)
			return puts("-1"), 0;
		vector <int> tmp;
		for (int i = 1; i <= n; i ++)
			if (cir[i])
				tmp.pb(siz[i] - sic[i] - sic[i]);
			else
				ans += abs(siz[i] - sic[i] - sic[i]);
		sort(tmp.begin(), tmp.end());
		for (auto x : tmp)
			ans += abs(x - tmp[tmp.size() / 2]);
		return printf("%lld\n", ans), 0;
	}
	int c = sic[rta] - (n >> 1);
	ans = abs(c);
	for (int i = 1; i <= n; i ++)
		if (i ^ rta)
			if (cir[i])
				ans += abs(siz[i] - sic[i] - sic[i] + c);
			else
				ans += abs(siz[i] - sic[i] - sic[i]);
	return printf("%lld\n", ans), 0;
}
