#include <bits/stdc++.h>
using namespace std;
struct node {
  int y, nex;
} a[2 * 110000];
int fir[110000], len, n, m, cnt, u, v, o, num, f[110000], g[110000], c[110000],
    A[110000];
long long ans;
void ins(int x, int y) {
  a[++len].y = y;
  a[len].nex = fir[x];
  fir[x] = len;
}
void dfs(int x, int fa) {
  for (int k = fir[x]; k; k = a[k].nex) {
    int y = a[k].y;
    if (y == fa) continue;
    if (c[y] == 0) {
      c[y] = -c[x];
      dfs(y, x);
    } else {
      u = x;
      v = y;
      if (c[x] == c[y])
        o = 1;
      else
        o = 0;
    }
  }
}
void dp(int x, int fa) {
  f[x] = c[x];
  for (int k = fir[x]; k; k = a[k].nex) {
    int y = a[k].y;
    if (y == fa || (x == u && y == v) || (x == v && y == u)) continue;
    dp(y, x);
    f[x] += f[y];
    g[x] += g[y];
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    ins(x, y);
    ins(y, x);
  }
  c[1] = 1;
  dfs(1, 0);
  if (m == n - 1) {
    dp(1, 0);
    if (f[1]) {
      printf("-1\n");
      return 0;
    }
    for (int i = 1; i <= n; i++) ans += abs(f[i]);
    printf("%lld\n", ans);
  } else if (o == 1) {
    dp(1, 0);
    if (f[1] % 2) {
      printf("-1\n");
      return 0;
    }
    int t = f[1];
    c[u] -= t / 2;
    c[v] -= t / 2;
    ans = abs(t / 2);
    dp(1, 0);
    for (int i = 1; i <= n; i++) ans += abs(f[i]);
    printf("%lld\n", ans);
  }
  return 0;
}
