#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()

#ifdef LOCAL
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else 
#define dump(x) true
#endif

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

template<class T, class U> void chmin(T& t, const U& u) { if (t > u) t = u; }
template<class T, class U> void chmax(T& t, const U& u) { if (t < u) t = u; }

template<class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os<<"("<<p.first<<","<<p.second<<")";
	return os;
}

template<class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
	os<<"{";
	rep(i, v.size()) {
		if (i) os<<",";
		os<<v[i];
	}
	os<<"}";
	return os;
}

class unionfind {
	vector<int> par, rank;

public:
	void init(int n) {
		par.resize(n);
		rank.resize(n);

		for (int i = 0; i < n; i++) {
			par[i] = i;
			rank[i] = 0;
		}
	}

	int find(int x) {
		if (par[x] == x) return x;
		else return par[x] = find(par[x]);
	}

	void unite(int x, int y) {
		x = find(x);
		y = find(y);
		if (x == y) return ;

		if (rank[x] < rank[y]) par[x] = y;
		else {
			par[y] = x;
			if (rank[x] == rank[y]) ++rank[x];
		}
	}

	bool same(int x, int y) { return (find(x) == find(y)); }
} uf;

const int maxn = 200010;
int depth[maxn];
int par[maxn];

void pre(int v, int p, int d, int dep, const VV<int>& g, V<int>& num) {
	depth[v] = dep;
	par[v] = p;

	if (d) {
		num[v]++;
	}
	for (int to : g[v]) if (to != p) {
		pre(to, v, d ^ 1, dep + 1, g, num);
	}
}

pii dfs(int v, int p, ll &res, const VV<int>& g, const V<int>& sv, const V<int>& gv, V<pii>& dp) {
	pii a(sv[v], gv[v]);
	for (int to : g[v]) if (to != p) {
		auto val = dfs(to, v, res, g, sv, gv, dp);
		res += abs(val.fi - val.se);
		a.fi += val.fi, a.se += val.se;
	}
	dp[v] = a;
	return a;
}

ll solve_tree(const VV<int>& g, V<int>& sv, V<int>& gv, V<pii>& dp, int root=0) {
	dump(sv);
	dump(gv);
	int sums = 0, sumg = 0;
	for (auto x : sv) {
		sums += x;
	}
	for (auto x : gv) {
		sumg += x;
	}

	if (sums != sumg) {
		return -1;
	}

	ll ans = 0;
	dfs(root, -1, ans, g, sv, gv, dp);

	return ans;
}

int main() {
	int N, M; cin >> N >> M;
	uf.init(N);
	VV<int> g(N);
	V<pii> es;

	int ng = -1;

	rep(i, M) {
		int a, b;
		cin >> a >> b;
		--a; --b;
		es.eb(a, b);
		if (uf.same(a, b)) {
			ng = i;
		} else {
			uf.unite(a, b);
			g[a].pb(b);
			g[b].pb(a);			
		}
	}

	if (ng == -1) {
		V<int> sv(N), gv(N);
		pre(0, -1, 0, 0, g, sv);
		pre(0, -1, 1, 0, g, gv);
		V<pii> dp(N);
		cout << solve_tree(g, sv, gv, dp) << endl;
		return 0;
	}

	int a, b; tie(a, b) = es[ng];
	V<int> sv(N), gv(N);
	pre(a, -1, 0, 0, g, sv);
	pre(a, -1, 1, 0, g, gv);

	if (depth[b] % 2 == 1) { //even cycle
		int c1 = 0, c2 = 0;
		for (int x : sv) {
			c1 += x;
		}
		for (int x : gv) {
			c2 += x;
		}
		if (c1 != c2) {
			puts("-1");
			return 0;
		}
		V<pii> dp(N);
		ll ans = solve_tree(g, sv, gv, dp, a);
		V<int> df;

		{
			int v = b;
			while (v != a) {
				df.pb(dp[v].fi - dp[v].se);
				v = par[v];
			}
			df.pb(0);
			sort(ALL(df));
			dump(df);
			for (int d : df) {
				ans -= abs(d);
			}
			dump(ans);
			for (int d : df) {
				ans += abs(d - df[df.size()/2]);
			}
		} 
		cout << ans << endl;
	} else { //odd cycle
		int c1 = 0, c2 = 0;
		for (int x : sv) {
			c1 += x;
		}
		for (int x : gv) {
			c2 += x;
		}	

		if ((c1 - c2) % 2 != 0)	{
			puts("-1");
			return 0;
		}

		int d = abs(c1 - c2) / 2;

		if (c1 < c2) {
			sv[a] += d;
			sv[b] += d;
		} else {
			gv[a] += d;
			gv[b] += d;		
		}
		V<pii> dp(N);
		cout << solve_tree(g, sv, gv, dp) + d << endl;
	}

	return 0;
}

Measured memory_bytes: 23028