//waz
#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)((x).size()))

typedef pair<int, int> PII;
typedef vector<int> VI;
typedef long long int64;
typedef unsigned int uint;
typedef unsigned long long uint64;

#define gi(x) ((x) = F())
#define gii(x, y) (gi(x), gi(y))
#define giii(x, y, z) (gii(x, y), gi(z))

int F()
{
	char ch;
	int x, a;
	while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-');
	if (ch == '-') ch = getchar(), a = -1;
	else a = 1;
	x = ch - '0';
	while (ch = getchar(), ch >= '0' && ch <= '9')
		x = (x << 1) + (x << 3) + ch - '0';
	return a * x;
}

const int N = 1e5 + 10;

VI edge[N];

int n, m;

namespace task1
{
	set<int> e[N][2];
	
	int fa[N], dep[N], check_cnt;
	
	void dfs1(int u)
	{
		dep[u] = dep[fa[u]] + 1;
		e[u][dep[u] & 1].insert(u);
		if (dep[u] & 1) ++check_cnt;
		else --check_cnt;
		for (auto v : edge[u])
		{
			if (v == fa[u]) continue;
			fa[v] = u;
			dfs1(v);
		}
	}
	
	long long ans;
	
	void dfs2(int u)
	{
		for (auto v : edge[u])
		{
			if (v == fa[u]) continue;
			dfs2(v);
			if (e[u][1].size() < e[v][1].size())
				e[u][1].swap(e[v][1]);
			for (auto x : e[v][1])
				e[u][1].insert(x);
			if (e[u][0].size() < e[v][0].size())
				e[u][0].swap(e[v][0]);
			for (auto x : e[v][0])
				e[u][0].insert(x);
		}
		if (e[u][1].size() && e[u][0].size())
		{
			int s = min(e[u][1].size(), e[u][0].size());
			set<int>::iterator it1 = e[u][1].begin(), it2 = e[u][0].begin();
			static PII stk[N]; int tp = 0;
			for (int i = 1; i <= s; ++i)
			{
				int v1 = *it1;
				int v2 = *it2;
				stk[++tp] = mp(v1, v2);
				ans += dep[v1] + dep[v2] - (dep[u] << 1);
				//cerr << v1 << ", " << v2 << ", " << u << endl;
				++it1, ++it2;
			}
			for (int i = 1; i <= tp; ++i)
				e[u][1].erase(stk[i].fi), e[u][0].erase(stk[i].se);
		}
	}
	
	void solve()
	{
		dfs1(1);
		if (check_cnt)
		{
			puts("-1");
			return;
		}
		dfs2(1);
		printf("%lld\n", ans);
	}
}

namespace task2
{
	bool vis[N], cir[N];
	int use_fa[N];
	vector<int> circle;
	bool exit_flag;
	void dfs1(int u)
	{
		vis[u] = 1;
		for (auto v : edge[u])
		{
			if (exit_flag) return;
			if (use_fa[u] == v) continue;
			if (vis[v])
			{
				while (u != v) circle.pb(u), cir[u] = 1, u = use_fa[u];
				circle.pb(v), cir[v] = 1;
				exit_flag = 1;
				return;
			}
			use_fa[v] = u;
			dfs1(v);
		}
	}
	
	int dep[N], fa[N];
	
	set<int> e[N][2];
	
	void dfs2(int u)
	{
		dep[u] = dep[fa[u]] + 1;
		e[u][dep[u] & 1].insert(u);
		for (auto v : edge[u])
		{
			if (v == fa[u]) continue;
			if (cir[v]) continue;
			fa[v] = u;
			dfs2(v);
		}
	}
	
	long long ans;
	
	void dfs3(int u)
	{
		for (auto v : edge[u])
		{
			if (v == fa[u]) continue;
			if (cir[v]) continue;
			dfs3(v);
			if (e[u][1].size() < e[v][1].size())
				e[u][1].swap(e[v][1]);
			for (auto x : e[v][1])
				e[u][1].insert(x);
			if (e[u][0].size() < e[v][0].size())
				e[u][0].swap(e[v][0]);
			for (auto x : e[v][0])
				e[u][0].insert(x);
		}
		if (e[u][1].size() && e[u][0].size())
		{
			int s = min(e[u][1].size(), e[u][0].size());
			set<int>::iterator it1 = e[u][1].begin(), it2 = e[u][0].begin();
			static PII stk[N]; int tp = 0;
			for (int i = 1; i <= s; ++i)
			{
				int v1 = *it1;
				int v2 = *it2;
				stk[++tp] = mp(v1, v2);
				ans += dep[v1] + dep[v2] - (dep[u] << 1);
				//cerr << v1 << ", " << v2 << ", " << u << endl;
				++it1, ++it2;
			}
			for (int i = 1; i <= tp; ++i)
				e[u][1].erase(stk[i].fi), e[u][0].erase(stk[i].se);
		}
	}
	
	int d[N];
	
	void solve()
	{
		dfs1(1);
		for (auto root : circle)
		{
			//cerr << root << endl;
			dfs2(root), dfs3(root);
			if (e[root][1].size())
			{
				for (auto v : e[root][1])
					++d[root], ans += dep[v] - 1;
			}
			else if (e[root][0].size())
			{
				for (auto v : e[root][0])
					--d[root], ans += dep[v] - 1;
			}
		}
		int vtx = SZ(circle);
		//cerr << "ok!" << endl;
		if (vtx % 2 == 0)
		{
			int sum = 0;
			for (int i = 0; i < vtx; ++i)
			{
				if (i & 1) d[circle[i]] = -d[circle[i]];
				sum += d[circle[i]];
			}
			if (sum) 
			{
				puts("-1");
				return;
			}
			static vector<int> t;
			t.pb(-d[circle[0]]);
			for (int i = 1; i < vtx; ++i)
			{
				d[circle[i]] += d[circle[i - 1]];
				t.pb(-d[circle[i]]);
			}
			sort(t.begin(), t.end());
			int v = t[SZ(t) >> 1];
			for (auto x : t)
				ans += abs(x - v);
			printf("%lld\n", ans);
		}
		else
		{
			int sum = 0;
			for (int i = 0; i < vtx; ++i)
			{
				if (i & 1) d[circle[i]] = -d[circle[i]];
				sum += d[circle[i]];
			}
			if (sum % 2 != 0)
			{
				puts("-1");
				return;
			}
			ans += abs(sum / 2);
			d[circle[0]] -= sum / 2;
			d[circle[vtx - 1]] += sum / 2;
			for (int i = 0; i < vtx - 1; ++i)
			{
				ans += abs(d[circle[i]]);
				d[circle[i + 1]] += d[circle[i]];
			}
			printf("%lld\n", ans);
		} 
	}
}

int main()
{
	gii(n, m);
	for (int i = 1; i <= m; ++i)
	{
		int u, v;
		gii(u, v);
		edge[u].push_back(v);
		edge[v].push_back(u);
	}
	if (m == n - 1)
	{
		task1::solve();
		return 0;
	}
	task2::solve();
	return 0;
}

Measured memory_bytes: 46208