#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int f[N], d[N], n, m, u, v, fa[N], cnt, x, y, a[N];
vector<int> G[N];
long long ans;
bool vis[N];
void dfs(int u) {
  f[u] = d[u] & 1 ? 1 : -1;
  vis[u] = 1;
  for (int &v : G[u])
    if (v ^ fa[u])
      if (vis[v])
        x = u, y = v;
      else
        d[v] = d[u] + 1, fa[v] = u, dfs(v), f[u] += f[v];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (1); i <= (m); ++i)
    scanf("%d%d", &u, &v), G[u].push_back(v), G[v].push_back(u);
  dfs(1);
  if (m == n - 1) {
    if (f[1]) return puts("-1"), 0;
    for (int i = (1); i <= (n); ++i) ans += abs(f[i]);
  } else if (!((d[x] ^ d[y]) & 1)) {
    if (f[1] & 1) return puts("-1"), 0;
    int t = f[1] >> 1;
    for (; x; x = fa[x]) f[x] -= t;
    ans = abs(t);
    for (; y; y = fa[y]) f[y] -= t;
    for (int i = (1); i <= (n); ++i) ans += abs(f[i]);
  } else {
    if (f[1]) return puts("-1"), 0;
    if (d[x] < d[y]) swap(x, y);
    for (; x ^ y; x = fa[x]) a[++cnt] = f[x], vis[x] = 2;
    a[++cnt] = 0;
    sort(a + 1, a + cnt + 1);
    for (int i = (1); i <= (n + 1); ++i)
      if (vis[x] == 2)
        ans += abs(f[i] - a[(cnt + 1) >> 1]);
      else
        ans += abs(f[i]);
  }
  printf("%lld\n", ans);
  return 0;
}
