#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int n, m, u, v, all;
vector<int> edge[maxn];
bool vis[maxn];
int sum[maxn], coef[maxn];
void predfs(int x, int p, int d) {
  sum[x] = d;
  vis[x] = true;
  all += sum[x];
  for (int i = 0; i < int(edge[x].size()); i++) {
    int y = edge[x][i];
    if (y == p) continue;
    if (!vis[y])
      predfs(y, x, -d);
    else
      u = x, v = y;
  }
}
void dfs(int x, int p) {
  for (int i = 0; i < int(edge[x].size()); i++) {
    int y = edge[x][i];
    if (y == p || (x == u && y == v) || (x == v && y == u)) continue;
    dfs(y, x);
    sum[x] += sum[y];
    coef[x] += coef[y];
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &u, &v);
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  predfs(1, 0, 1);
  if (m == n - 1) {
    if (all)
      puts("-1");
    else {
      dfs(1, 0);
      long long ans = 0;
      for (int i = 1; i <= n; i++) ans += abs(sum[i]);
      printf("%lld\n", ans);
    }
  } else if (sum[u] != sum[v]) {
    if (all)
      puts("-1");
    else {
      coef[u]++;
      coef[v]--;
      dfs(1, 0);
      vector<int> val;
      val.push_back(0);
      for (int i = 1; i <= n; i++) {
        if (coef[i] > 0)
          val.push_back(-sum[i]);
        else
          val.push_back(sum[i]);
      }
      sort(val.begin(), val.end());
      int best = val[int(val.size()) / 2];
      long long ans = best;
      for (int i = 1; i <= n; i++) ans += abs(best * coef[i] + sum[i]);
      printf("%lld\n", ans);
    }
  } else {
    if (all & 1)
      puts("-1");
    else {
      long long ans = abs(all / 2);
      sum[u] -= all / 2;
      sum[v] -= all / 2;
      dfs(1, 0);
      for (int i = 1; i <= n; i++) ans += abs(sum[i]);
      printf("%lld\n", ans);
    }
  }
  return 0;
}
