#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, x, y, ioc, w[100005], w2[100005], vis[100005], px, py;
vector<int> bi[100005], seq;
void dfs(int x, int fa) {
  vis[x] = 1;
  for (__typeof((bi[x]).begin()) it = (bi[x]).begin(); it != (bi[x]).end();
       it++) {
    if (*it != fa) {
      if (vis[*it]) {
        px = x;
        py = *it;
      } else {
        w[*it] = -w[x];
        dfs(*it, x);
      }
    }
  }
}
void dfs2(int x, int fa) {
  for (__typeof((bi[x]).begin()) it = (bi[x]).begin(); it != (bi[x]).end();
       it++) {
    if (*it == fa) continue;
    if (px == x && py == *it) continue;
    if (py == x && px == *it) continue;
    dfs2(*it, x);
    w[x] += w[*it];
    w2[x] += w2[*it];
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for ((i) = 1; (i) <= (m); (i)++) {
    scanf("%d%d", &x, &y);
    bi[x].push_back(y);
    bi[y].push_back(x);
  }
  w[1] = 1;
  dfs(1, 0);
  int cnt = 0, ans = 0;
  for ((i) = 1; (i) <= (n); (i)++) {
    cnt += w[i];
  }
  if (n != m) {
    if (cnt) {
      puts("-1");
      return 0;
    }
  } else {
    if (w[px] == w[py]) {
      if (cnt & 1) {
        puts("-1");
        return 0;
      }
      ans = abs(cnt) / 2;
      w[px] -= cnt / 2;
      w[py] -= cnt / 2;
    } else {
      if (cnt) {
        puts("-1");
        return 0;
      }
      w2[px] = 1;
      w2[py] = 1;
    }
  }
  dfs2(1, 0);
  seq.push_back(0);
  for ((i) = 1; (i) <= (n); (i)++) {
    if (!w2[i]) {
      ans += abs(w[i]);
    } else {
      seq.push_back(w[i]);
    }
  }
  sort(seq.begin(), seq.end());
  int t = seq[seq.size() / 2];
  for (__typeof((seq).begin()) it = (seq).begin(); it != (seq).end(); it++) {
    ans += abs(*it - t);
  }
  printf("%d\n", ans);
  return 0;
}
