#include <bits/stdc++.h>
using namespace std;
vector<int> g[100100];
int deg[100100], val[100100], tv[100100];
int id[100100], cnt;
bool del[100100];
int main() {
  long long ans = 0;
  int n, m, x, y;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    g[x].push_back(y);
    g[y].push_back(x);
    deg[x]++;
    deg[y]++;
  }
  queue<int> q;
  for (int i = 1; i <= n; i++) {
    if (deg[i] == 1) q.push(i);
    val[i] = 1;
  }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (deg[u] != 1) {
      continue;
    }
    del[u] = true;
    ans += abs(val[u]);
    for (int i = 0; i < g[u].size(); i++) {
      int v = g[u][i];
      if (del[v]) continue;
      val[v] -= val[u];
      if (--deg[v] == 1) q.push(v);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!del[i]) {
      while (1) {
        del[i] = true;
        id[cnt++] = i;
        int nxt = -1;
        for (int j = 0; j < g[x].size(); j++) {
          if (del[g[x][j]]) continue;
          nxt = g[x][j];
          break;
        }
        if (nxt < 0) break;
        i = nxt;
      }
      break;
    }
  }
  if (cnt == 1) {
    if (val[id[0]])
      puts("-1");
    else
      printf("%lld\n", ans);
  } else if (cnt & 1) {
    long long tmp = 0;
    for (int i = 0; i < cnt; i++) {
      tmp += val[id[i]];
    }
    if (tmp & 1)
      puts("-1");
    else {
      tmp >>= 1;
      for (int i = 1; i < cnt; i += 2) {
        tmp -= val[id[i]];
      }
      for (int i = 0; i < cnt; i++) {
        ans += abs(tmp);
        tmp = val[id[i]] - tmp;
      }
      printf("%lld\n", ans);
    }
  } else {
    long long tmp = 0, now = 0;
    for (int i = 0; i < cnt; i++) {
      if (i & 1)
        tmp += val[id[i]], tv[i] = now;
      else
        tmp -= val[id[i]], tv[i] = -now;
      now = val[id[i]] - now;
    }
    if (tmp)
      puts("-1");
    else {
      sort(tv, tv + cnt);
      for (int i = 0; i < cnt - 1 - i; i++) {
        ans += tv[cnt - 1 - i] - tv[i];
      }
      printf("%lld\n", ans);
    }
  }
  return 0;
}
