#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

typedef pair <int, int> pii;
#define V first
#define U second
#define ID second

const int N = 1e5 + 5;
int n, m;
int deep[N];
int token[N];
int capa[N];
pii edge[N];
bool in_cycle[N];
bool mark[N];
int id_e = -1;
vector <int> path;
vector <pii> G[N];

void dfs (int v = 0, int par = 0) {
	deep[v] = 1 + deep[par];
	for (pii u: G[v])
		if (u.ID != id_e && u.V != par)
			dfs(u.V, v);
}

void calc (int v = 0, int par = 0) {
	for (pii u: G[v])
		if (u.ID != id_e && u.V != par) {
			calc(u.V, v);
			token[v] += token[u.V];
			capa[v] += capa[u.V];
		}
}

bool find_cycle (int v = 0, int par = 0) {
	mark[v] = true;
	for (pii u: G[v])
		if (!mark[u.V]) {
			path.push_back(u.ID);
			if (find_cycle(u.V, v))
				return true;

			path.pop_back();
		}
		else {
			if (u.V != par){
				id_e = u.ID;
				return true;
			}
		}

	return false;
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> edge[i].V >> edge[i].U;
		edge[i].V--, edge[i].U--;

		G[edge[i].V].push_back({edge[i].U, i});
		G[edge[i].U].push_back({edge[i].V, i});
	}

	if (m == n - 1) {
		dfs();
		int mod[2] = {0};
		for (int i = 0; i < n; i++)
			mod[deep[i] & 1]++;
		
		if (mod[0] != mod[1])
			return cout << "-1\n", 0;
		
		for (int i = 0; i < n; i++)
			if (deep[i] & 1)
				token[i]++;
			else
				capa[i]++;

		calc();
		int ans = 0;
		for (int i = 0; i < m; i++) {
			if (deep[edge[i].U] < deep[edge[i].V])
				swap(edge[i].V, edge[i].U);

 			ans += abs(token[edge[i].U] - capa[edge[i].U]);
		}

		return cout << ans << "\n", 0;
	}

	find_cycle();
	int ver = edge[id_e].V;
	path.clear();
	memset(mark, 0, sizeof mark);

	find_cycle(ver);

	if (path.size() & 1) {
		for (int id: path)
			in_cycle[id] = true;
		in_cycle[id_e] = true;

		dfs();
		int mod[2] = {0};
		for (int i = 0; i < n; i++)
			mod[deep[i] & 1]++;
		
		if (mod[0] != mod[1])
			return cout << "-1\n", 0;

		for (int i = 0; i < n; i++)
			if (deep[i] & 1)
				token[i]++;
			else
				capa[i]++;

		calc();
		vector <int> pp;
		pp.push_back(0);
		int ans = 0;

		for (int i = 0; i < m; i++) {
			if (deep[edge[i].U] < deep[edge[i].V])
				swap(edge[i].V, edge[i].U);

			if (id_e == i)
				continue;

			if (in_cycle[i])
				pp.push_back(token[edge[i].U] - capa[edge[i].U]);
			else
				ans += abs(token[edge[i].U] - capa[edge[i].U]);
		}

		sort(pp.begin(), pp.end());
		for (int i = 0; i < pp.size(); i++)
			ans += abs(pp[i] - pp[pp.size() / 2]);
		
		cout << ans << "\n";
		return 0;
	}
	
	for (int id: path)
		in_cycle[id] = true;
	in_cycle[id_e] = true;

	dfs();
	int mod[2] = {0};
	for (int i = 0; i < n; i++)
		mod[deep[i] & 1]++;

	if ((mod[0] - mod[1]) % 2)
		return cout << "-1\n", 0;

	int ans = abs(mod[0] - mod[1]) / 2;
	for (int i = 0; i < n; i++)
		if (deep[i] % 2 == (mod[0] >= mod[1]))
			token[i]++;
		else
			capa[i]++;

	token[edge[id_e].V] += ans;
	token[edge[id_e].U] += ans;
	calc();
	for (int i = 0; i < m; i++) {
		if (deep[edge[i].U] < deep[edge[i].V])
			swap(edge[i].V, edge[i].U);
		if (i == id_e)
			continue;
		
		ans += abs(token[edge[i].U] - capa[edge[i].U]);
	}
	cout << ans << "\n";
	return 0;
}

Measured memory_bytes: 21180