#include <bits/stdc++.h>
using namespace std;
vector<int> pat[100000];
bool flag[100000];
bool iscirc[100000];
vector<int> nowp;
vector<int> circ;
bool found;
void dfs(int node, int par) {
  if (found) return;
  if (flag[node]) {
    for (int i = nowp.size() - 1; i >= 0; i--) {
      circ.push_back(nowp[i]);
      iscirc[nowp[i]] = true;
      if (nowp[i] == node) break;
    }
    found = true;
    return;
  }
  flag[node] = true;
  nowp.push_back(node);
  for (int i = 0; i < pat[node].size(); i++) {
    if (pat[node][i] != par) dfs(pat[node][i], node);
  }
  nowp.pop_back();
}
long long ans = 0;
int get(int node) {
  flag[node] = true;
  int s = 1;
  for (int i = 0; i < pat[node].size(); i++) {
    if (!iscirc[pat[node][i]] && !flag[pat[node][i]]) s -= get(pat[node][i]);
  }
  if (!iscirc[node]) ans += abs(s);
  return s;
}
void no() {
  printf("-1\n");
  exit(0);
}
int main() {
  int num, way;
  scanf("%d%d", &num, &way);
  for (int i = 0; i < way; i++) {
    int za, zb;
    scanf("%d%d", &za, &zb);
    za--, zb--;
    pat[za].push_back(zb);
    pat[zb].push_back(za);
  }
  if (way == num - 1) {
    int t = pat[0][0];
    iscirc[0] = iscirc[t] = true;
    circ.push_back(0);
    circ.push_back(t);
  } else
    dfs(0, -1);
  vector<int> r;
  for (int i = 0; i < circ.size(); i++) r.push_back(get(circ[i]));
  if (r.size() % 2 == 0) {
    long long s1 = 0, s2 = 0;
    for (int i = 0; i < r.size(); i += 2) s1 += r[i], s2 += r[i + 1];
    if (s1 != s2) no();
    vector<int> z;
    int now = 0;
    for (int i = 0; i < r.size(); i++) {
      z.push_back((i & 1) ? now : -now);
      now = r[i] - now;
    }
    sort(z.begin(), z.end());
    int m = z[z.size() / 2];
    for (int i = 0; i < z.size(); i++) ans += abs(m - z[i]);
  } else {
    abort();
    long long s = 0;
    for (int i = 0; i < r.size(); i++) s += r[i];
    if (s % 2 != 0) no();
    long long z = 0;
    for (int i = 0; i < r.size() - 2; i += 2) z += r[i];
    for (int i = 0; i < r.size(); i++) {
      ans += abs(s / 2 - z);
      z -= r[i * 2 % r.size()];
      z += r[(i * 2 + r.size() - 1) % r.size()];
    }
  }
  printf("%lld\n", ans);
}
