#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, m, ans, stx, sty, fx, fy;
int to[N << 1], nxt[N << 1], lnk[N], cnt;
int dep[N], fa[N], s[N], a[N], top;
void add(int x, int y) { to[++cnt] = y, nxt[cnt] = lnk[x], lnk[x] = cnt; }
int getfa(int x) { return fa[x] == x ? x : fa[x] = getfa(fa[x]); }
void dfs(int x, int f, int val) {
  dep[x] = dep[f] + 1;
  fa[x] = f;
  s[x] = val;
  for (int i = lnk[x]; i; i = nxt[i]) {
    int y = to[i];
    if (y != f) dfs(y, x, -val), s[x] += s[y];
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) fa[i] = i;
  stx = sty = 1;
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    if ((fx = getfa(x)) != (fy = getfa(y)))
      fa[fx] = fy;
    else {
      stx = x, sty = y;
      continue;
    }
    add(x, y), add(y, x);
  }
  dfs(stx, 0, 1);
  if (n == m) {
    for (int i = sty; i; i = fa[i]) a[++top] = s[i];
    if ((dep[stx] - dep[sty]) & 1) {
      if (s[stx]) return puts("-1"), 0;
      sort(a + 1, a + top + 1);
      for (int i = sty; i; i = fa[i]) s[i] -= a[top >> 1];
    } else {
      if (s[stx] & 1) return puts("-1"), 0;
      for (int i = sty; i; i = fa[i]) s[i] -= s[top] >> 1;
    }
  } else if (s[stx])
    return puts("-1"), 0;
  for (int i = 1; i <= n; i++) ans += abs(s[i]);
  return printf("%d\n", ans), 0;
}
