#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>

using namespace std;

const int MAXN = 100000 + 100;

int n, m;
vector<int> edg[MAXN];
bool vis[MAXN], col[MAXN], onc[MAXN];
int ccnt[MAXN][2];
vector<int> cir;
int U, V;

int find_circle(int x, int fa)
{
	vis[x] = true;
	for(int i = 0; i < edg[x].size(); i++)
		if(edg[x][i] != fa)
		{
			int y = edg[x][i], tmp = vis[y] ? y : find_circle(y, x);
			if(tmp)
			{
				onc[x] = true, cir.push_back(x);
				return x == tmp ? 0 : tmp;
			}
		}
	return 0;
}

void dfs_color(int x, int fa, int c)
{
	col[x] = c, ccnt[x][c] = 1;
	for(int i = 0; i < edg[x].size(); i++)
		if(edg[x][i] != fa)
		{
			int y = edg[x][i];
			dfs_color(y, x, c ^ 1);
			ccnt[x][0] += ccnt[y][0], ccnt[x][1] += ccnt[y][1];
		}
}

namespace TreeCase
{
	int dfs(int x, int fa)
	{
		int ret = 0;
		for(int i = 0; i < edg[x].size(); i++)
			if(edg[x][i] != fa)
			{
				int y = edg[x][i];
				ret += dfs(y, x) + abs(ccnt[y][0] - ccnt[y][1]);
			}
		return ret;
	}
	void work()
	{
		dfs_color(1, 0, 0);
		if(ccnt[1][0] != ccnt[1][1])
		{
			cout << -1 << endl;
			return ;
		}
		int ans = dfs(1, 0);
		cout << ans << endl;
	}
}

namespace CircleCase
{
	namespace EvenCase
	{
		int dfs(int x, int fa)
		{
			int ret = 0;
			for(int i = 0; i < edg[x].size(); i++)
				if(edg[x][i] != fa && !onc[edg[x][i]])
				{
					int y = edg[x][i];
					ret += dfs(y, x) + abs(ccnt[y][0] - ccnt[y][1]);
				}
			return ret;
		}
		void work()
		{
			dfs_color(V, 0, 0);
			if(ccnt[V][0] != ccnt[V][1])
			{
				cout << -1 << endl;
				return ;
			}
			int ans = 0;
			for(int i = 0; i < cir.size(); i++)
				ans += dfs(cir[i], 0);
			vector<int> arr;
			arr.push_back(0);
			for(int i = 1; i < cir.size(); i++)
				arr.push_back(ccnt[cir[i]][1] - ccnt[cir[i]][0]);
			sort(arr.begin(), arr.end());
			int X = arr[arr.size() / 2];
			for(int i = 0; i < arr.size(); i++)
				ans += abs(X - arr[i]);
			cout << ans << endl;
		}
	}

	namespace OddCase
	{
		int dfs(int x, int fa)
		{
			int ret = 0;
			for(int i = 0; i < edg[x].size(); i++)
				if(edg[x][i] != fa)
				{
					int y = edg[x][i];
					ret += dfs(y, x) + abs(ccnt[y][0] - ccnt[y][1]);
					ccnt[x][0] += ccnt[y][0], ccnt[x][1] += ccnt[y][1];
				}
			return ret;
		}
		void work()
		{
			dfs_color(1, 0, 0);
			if(ccnt[1][0] % 2 != ccnt[1][1] % 2)
			{
				cout << -1 << endl;
				return ;
			}
			if(ccnt[1][0] < ccnt[1][1])
				for(int i = 1; i <= n; i++)
					col[i] ^= 1, swap(ccnt[i][0], ccnt[i][1]);
			int k = (ccnt[1][0] - ccnt[1][1]) / 2;
			memset(ccnt, 0, sizeof(ccnt));
			for(int i = 1; i <= n; i++)
				ccnt[i][col[i]] = 1;
			ccnt[U][1] += k, ccnt[V][1] += k;
			int ans = dfs(1, 0) + k;
			cout << ans << endl;
		}
	}

	void work()
	{
		find_circle(1, 0);
		U = cir.back(), V = cir.front();
		edg[U].erase(find(edg[U].begin(), edg[U].end(), V));
		edg[V].erase(find(edg[V].begin(), edg[V].end(), U));
		if(cir.size() & 1)
			OddCase::work();
		else
			EvenCase::work();
	}
}

int main()
{
	ios::sync_with_stdio(false);
	// freopen("1.in", "r", stdin);
	// freopen("1.out", "w", stdout);

	cin >> n >> m;
	for(int i = 1; i <= m; i++)
	{
		int x, y;
		cin >> x >> y;
		edg[x].push_back(y), edg[y].push_back(x);
	}

	if(m == n - 1)
		TreeCase::work();
	else
		CircleCase::work();

	return 0;
}


Measured memory_bytes: 15736