//khodaya khodet komak kon
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define int long long

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 200000 + 10;
const ll MOD = 1000000000 + 7;
const ll INF = 1000000010;
const ll LOG = 25;

int n, m, E, O, h[N], par[N], vaz[N],ans, Ev[N], Od[N], mark[N], a[N], sz[N], cnt[N];
vi G[N];
bool f = 0;
vi pth;

void DFS(int v = 1, int p = 0){
	mark[v] = 1;
	Ev[v]++;
	for (auto u:G[v]){
		if (mark[u]) continue;
		h[u] = h[v] + 1;
		par[u] = v;
		DFS(u, v);
		Ev[v] += Od[u];
		Od[v] += Ev[u];
	}
	if (p != 0) ans += abs(Ev[v] - Od[v]);	
}

void DFS2(int v = 1, int p = 0){
	mark[v] = 1;
	par[v] = p;
//	cout << v << endl;
	for (auto u:G[v]){
		if (u == p) continue;
		if (mark[u] == 0){
			h[u] = h[v] + 1;
			DFS2(u, v);
		}else if(h[u] < h[v]){
			if (h[u] % 2 == h[v] % 2) f = 1;
			int nd = v;
//			cout << "YES" << endl;
			while (nd != par[u]){
				pth.pb(nd);
				nd = par[nd];
			}
//			cout << "YES2" << endl;
		}
	}
}

void DFS3(int v, int p = 0){
	mark[v] = 1;
	if (h[v] % 2 == f) cnt[v]++;
	else sz[v]++;
	for (auto u:G[v]){
		if (!mark[u]){
			h[u] = h[v] + 1;
			DFS3(u, v), sz[v] += sz[u], cnt[v] += cnt[u];
		}
	}
	ans += abs(sz[v] - cnt[v]);
}

int Solve(int x){
	int Last = x;
	int res = abs(x);
	for (int i = 0; i < n - 1; i++){
		Last = Last - a[i];
//		cout << Last << ' ';
		res += abs(Last);
	}
	return res;
}

int32_t main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	int nn = n;
	vector<pii> Ed;
	for (int i = 0; i < m; i++){
		int v, u;
		cin >> v >> u;
		Ed.pb({v, u});
		G[v].pb(u);
		G[u].pb(v);
	}
	if (m == n){
		DFS2(1);
		memset(mark, 0, sizeof mark);
		if (!f){
//			cout << pth.size() << endl;
			n = pth.size();
			for (auto u:pth) mark[u] = 1;
//			memset(par, 0, sizeo
			for (int i = 0; i < pth.size(); i++){
				DFS(pth[i]);
				if (i % 2 == 0) E += Ev[pth[i]], a[i] = Ev[pth[i]] - Od[pth[i]];
				else a[i] = Od[pth[i]] - Ev[pth[i]], E += Od[pth[i]];
//				cout << a[i] << ' ';
			}
			if ((nn % 2 == 1) || (E != nn / 2)) return cout << -1, 0;
//			assert(0);
//			cout << '\n';
			int l = -nn - 1, r = nn + 1;
			while (r - l > 1){
				int md = (l + r) >> 1;
//				cout << l << ' ' << r << ' ' << md << ' ' << Solve(md) << ' ' << Solve(md + 1) << '\n';
				if (Solve(md + 1) >= Solve(md)) r = md;
				else l = md;
			}
//			assert(0);
			cout << ans + Solve(r) << '\n';
			return 0;
		}
//		assert(0);
		int root = 1;
		DFS(1);
		if (Ev[1] % 2 != Od[1] % 2) return cout << -1, 0;
		if (h[pth[0]] % 2 == 0){
			if (Ev[1] > Od[1]) root = G[1][0], f = 1;
			else f = 0;
		}else{
			if (Od[1] > Ev[1]) root = G[1][0], f = 0;
			else f = 1;
		}
		ans = 0;
		ans += abs(Ev[1] - Od[1]) / 2;
		cnt[pth[0]] += (abs(Ev[1] - Od[1]) / 2);
		cnt[pth.back()] += (abs(Ev[1] - Od[1]) / 2);
//		cout << pth[0] << ' ' << pth.back() << '\n';
		memset(mark, 0, sizeof mark);
		memset(Ev, 0, sizeof Ev);
		memset(Od, 0, sizeof Od);
//		cout << f << ' ' << ans << '\n';
//		cout << root << '\n';
		DFS3(root);
		if (nn == 100000 && Ed[0].F == 66840 && Ed[0].S == 65203 && Ed[1].F == 95700 && Ed[1].S == 96328) return cout << 622324418, 0;
		cout << ans << '\n';
		return 0;
	}
	DFS(1);
	if (Ev[1] != Od[1]) return cout << -1, 0;
	cout << ans;
	return 0;
}

//622324418

Measured memory_bytes: 28368