// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

int n, m, a, b, dp[N], cu, cv, vis[N], f[N], in[N], p[N], c, col[N];

vector<int> g[N];

void dfs(int u, int fa) {

    f[u] = fa;

    vis[u] = 1; col[u] = col[fa] ^ 1;

    dp[u] = col[u] ? 1 : -1;

    for(auto v : g[u]) if(v != fa) {

        if(!vis[v]) dfs(v, u), dp[u] += dp[v];

        else cu = u, cv = v;

    }

}

void tree() {

    if(dp[1] != 0) { printf("-1\n"); return; }

    int ans = 0;

    for(int i = 1; i <= n; ++i) ans += abs(dp[i]);

    printf("%d\n", ans);

}

void even() {

    if(dp[1] != 0) { printf("-1\n"); return; }

    while(cu != cv) p[++c] = dp[cv], in[cv] = 1, cv = f[cv];

    sort(p + 1, p + 1 + c);

    int mid = p[(c + 1) / 2];

    int ans = abs(mid);

    for(int i = 1; i <= n; ++i) if(!in[i]) ans += abs(dp[i]);

    for(int i = 1; i <= c; ++i) ans += abs(p[i] - mid);

    printf("%d\n", ans);

}

void odd() {

    if(dp[1] % 2 != 0) { printf("-1\n"); return; }

    int x = -dp[1] / 2;

    while(cu) dp[cu] += x, cu = f[cu];

    while(cv) dp[cv] += x, cv = f[cv];

    int ans = abs(x);

    for(int i = 1; i <= n; ++i) ans += abs(dp[i]);

    printf("%d\n", ans);

}

int main() {

    scanf("%d %d", &n, &m);

    for(int i = 1; i <= m; ++i) scanf("%d %d", &a, &b), g[a].push_back(b), g[b].push_back(a);

    dfs(1, 0);

    if(m == n - 1) tree();

    else if(col[cu] ^ col[cv]) even();

    else odd();

    return 0;

}


// Optimized solution to the same problem (faster):
#include <cmath>

#include <cstdio>

#include <cstring>

#include <iostream>

#include <algorithm>

using namespace std;

#define N 100005

#define ll long long

ll ans;

int n, m, S, T, A[N], siz[N];

int stk[N], top;

int tar[N << 1], nex[N << 1], fir[N], cnt;

bool odd;

void Add(int x, int y)

{

	++cnt;

	tar[cnt] = y;

	nex[cnt] = fir[x];

	fir[x] = cnt;

}

void Dfs1(int x, int fa)

{

	for (int i = fir[x]; i; i = nex[i])

	{

		int v = tar[i];

		if (v == fa)

			continue;

		if (siz[v])

		{

			if (siz[x] == siz[v])

				odd = 1;

			S = x, T = v;

		}

		else

		{

			siz[v] = -siz[x];

			Dfs1(v, x);

		}

	}

}

void Dfs2(int x, int fa)

{

	for (int i = fir[x]; i; i = nex[i])

	{

		int v = tar[i];

		if (v == fa || (x == S && v == T) || (x == T && v == S))

			continue;

		Dfs2(v, x);

		siz[x] += siz[v];

		A[x] += A[v];

	}

}

int main()

{

	scanf("%d%d", &n, &m);

	for (int i = 1; i <= m; i++)

	{

		int x, y;

		scanf("%d%d", &x, &y);

		Add(x, y), Add(y, x);

	}

	siz[1] = 1, Dfs1(1, 0);

	int sum = 0;

	for (int i = 1; i <= n; i++)

		sum += siz[i];

	if (m == n - 1)

	{

		if (sum)

		{

			puts("-1");

			return 0;

		}

	}

	else

	{

		if (odd)

		{

			if (sum & 1)

			{

				puts("-1");

				return 0;

			}

			ans += abs(sum>>1);

			siz[S] -= sum >> 1;

			siz[T] -= sum >> 1;

		}

		else

		{

			if (sum)

			{

				puts("-1");

				return 0;

			}

			else

				A[S] = 1, A[T] = -1;

		}

	}

	Dfs2(1, 0);

	for (int i = 1; i <= n; i++)

	{

		if (A[i])

			stk[++top] = A[i] * siz[i];

		else

			ans += abs(siz[i]);

	}

	stk[++top] = 0;

	sort(stk + 1, stk + top + 1);

	int mid = stk[(top + 1) >> 1];

	for (int i = 1; i <= top; i++)

		ans += abs(stk[i] - mid);

	printf("%lld\n", ans);

}


// Fastest known solution for this problem: