#include <bits/stdc++.h>
using namespace std;
int n, m, ly, s[100005], b[100005], vis[100005], sum, tar, flag, Ans;
int to[100005 * 2], fst[100005], nxt[100005 * 2], hz[100005 * 2], l = 1;
void biu() {
  puts("-1");
  exit(0);
}
void link(int x, int y) {
  to[++l] = y;
  nxt[l] = fst[x];
  fst[x] = l;
  to[++l] = x;
  nxt[l] = fst[y];
  fst[y] = l;
}
struct T {
  int x, y;
} a[100005];
T operator+(T a, T b) { return (T){a.x + b.x, a.y + b.y}; }
void Dfs(int x, int y, int f) {
  a[x].y = y;
  b[x] = y ^ 1;
  vis[x] = 1;
  sum += y;
  tar += y ^ 1;
  for (int i = fst[x]; i; i = nxt[i])
    if (to[i] != f) {
      if (!vis[to[i]])
        Dfs(to[i], y ^ 1, x);
      else
        flag = i;
    }
}
void Get(int x, int f) {
  for (int i = fst[x]; i; i = nxt[i])
    if (to[i] != f && !hz[i]) {
      Get(to[i], x);
      a[x] = a[x] + a[to[i]];
      b[x] = b[x] + b[to[i]];
    }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    link(x, y);
  }
  Dfs(1, 1, 0);
  if (flag) {
    int u = to[flag], v = to[flag ^ 1];
    if (a[u].y == a[v].y) {
      if ((tar - sum) % 2) biu();
      if (a[u].y)
        a[u].y += (tar - sum) / 2, a[v].y += (tar - sum) / 2;
      else
        a[u].y -= (tar - sum) / 2, a[v].y -= (tar - sum) / 2;
      Ans += (tar - sum) / 2;
    } else {
      if (sum != tar) biu();
      a[u].x++;
      a[v].x--;
      s[++ly] = 0;
    }
    hz[flag] = hz[flag ^ 1] = 1;
  } else if (sum != tar)
    biu();
  Get(1, 0);
  for (int i = 2; i <= n; i++) {
    a[i].y -= b[i];
    if (a[i].x)
      s[++ly] = a[i].x == 1 ? a[i].y : -a[i].y;
    else
      Ans += abs(a[i].y);
  }
  sort(s + 1, s + ly + 1);
  for (int i = 1; i <= ly; i++) Ans += abs(s[i] - s[ly / 2]);
  printf("%d\n", Ans);
}
