#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n, m, v[maxn], fa[maxn];
long long cnt[maxn][2], res, ans = 1e18;
vector<int> g[maxn];
int p1, p2, len;
void dfs(int x, int d = 0) {
  ++cnt[x][d & 1];
  for (int u = 0; u < g[x].size(); ++u) {
    if (g[x][u] == fa[x]) continue;
    if (x == p1 && g[x][u] == p2) continue;
    if (x == p2 && g[x][u] == p1) continue;
    fa[g[x][u]] = x;
    dfs(g[x][u], d + 1);
    cnt[x][0] += cnt[g[x][u]][0];
    cnt[x][1] += cnt[g[x][u]][1];
    res += abs(cnt[g[x][u]][0] - cnt[g[x][u]][1]);
  }
}
void dfs2(int x, int p = 0) {
  v[x] = v[p] + 1;
  for (int u = 0; u < g[x].size(); ++u) {
    if (g[x][u] == p) continue;
    if (v[g[x][u]]) {
      p1 = x;
      p2 = g[x][u];
      len = v[x] - v[g[x][u]] + 1;
    } else
      dfs2(g[x][u], x);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  if (m == n - 1) {
    dfs(1);
    long long ans = res;
    if (cnt[1][0] != cnt[1][1]) return 0 * puts("-1");
    printf("%lld\n", ans);
  } else {
    dfs2(1);
    dfs(1);
    if (len & 1) {
      if (abs(cnt[1][0] - cnt[1][1]) & 1) return 0 * puts("-1");
      long long del = (cnt[1][0] - cnt[1][1]) / 2;
      memset(cnt, 0, sizeof(cnt));
      cnt[p1][0] -= del;
      cnt[p2][0] -= del;
      res = 0;
      dfs(1);
      long long ans = res + abs(del);
      printf("%lld\n", ans);
    } else {
      fa[1] = 0;
      memset(cnt, 0, sizeof(cnt));
      dfs(1, 0);
      if (cnt[1][0] != cnt[1][1]) {
        puts("-1");
        return 0;
      }
      int l = -1e6, r = 1e6;
      while (l + 10 < r) {
        int midl = floor((2 * l + r) / 3.0), midr = ceil((l + 2 * r) / 3.0),
            resl, resr;
        res = abs(midl);
        memset(cnt, 0, sizeof(cnt));
        cnt[p1][1] = midl;
        cnt[p2][1] = -midl;
        dfs(1, 0);
        resl = res;
        res = abs(midr);
        memset(cnt, 0, sizeof(cnt));
        cnt[p1][1] = midr;
        cnt[p2][1] = -midr;
        dfs(1, 0);
        resr = res;
        if (resl <= resr) {
          r = midr - 1;
          if (ans > resl) ans = resl;
        } else {
          l = midl + 1;
          if (ans > resr) ans = resr;
        }
      }
      for (int i = l; i <= r; ++i) {
        memset(cnt, 0, sizeof(cnt));
        cnt[p1][0] += i;
        cnt[p2][1] += i;
        res = 0;
        dfs(1, 0);
        ans = min(ans, res + abs(i));
      }
      printf("%lld\n", ans);
    }
  }
}
