#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
int n, m, tot = 1, flag, key, cnt;
int pre[maxn * 2 + 8], now[maxn + 8], son[maxn * 2 + 8];
int dep[maxn + 8], fa[maxn + 8], f[maxn + 8], v[maxn + 8], tmp[maxn + 8];
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-') f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
  return x * f;
}
void add(int u, int v) {
  pre[++tot] = now[u];
  now[u] = tot;
  son[tot] = v;
}
void check(int x) {
  dep[x] = dep[fa[x]] + 1;
  v[x] = (dep[x] & 1) * 2 - 1;
  f[x] = v[x];
  for (int p = now[x]; p; p = pre[p]) {
    int child = son[p];
    if (fa[x] == child) continue;
    if (fa[child]) {
      flag = ((dep[x] - dep[child] + 1) & 1) + 1, key = p;
      continue;
    }
    fa[child] = x;
    check(child);
    f[x] += f[child];
  }
}
void dfs(int x) {
  f[x] = v[x];
  for (int p = now[x]; p; p = pre[p]) {
    int child = son[p];
    if ((p | 1) == (key | 1) || fa[x] == child) continue;
    dfs(child);
    f[x] += f[child];
  }
}
int main() {
  n = read(), m = read();
  for (int i = 1; i <= m; i++) {
    int u = read(), v = read();
    add(u, v), add(v, u);
  }
  fa[1] = 1;
  check(1);
  if (!flag) {
    if (f[1]) {
      puts("-1");
      return 0;
    }
    long long ans = 0;
    for (int i = 1; i <= n; i++) ans += abs(f[i]);
    printf("%lld\n", ans);
  }
  if (flag == 2) {
    if (f[1] & 1) {
      puts("-1");
      return 0;
    }
    v[son[key]] -= f[1] / 2;
    v[son[key ^ 1]] -= f[1] / 2;
    long long ans = abs(f[1] / 2);
    dfs(1);
    for (int i = 1; i <= n; i++) ans += abs(f[i]);
    printf("%lld\n", ans);
  }
  if (flag == 1) {
    if (dep[son[key]] < dep[son[key ^ 1]]) key ^= 1;
    int x = son[key];
    tmp[++cnt] = 0;
    while (x != son[key ^ 1]) tmp[++cnt] = f[x], x = fa[x];
    sort(tmp + 1, tmp + cnt + 1);
    long long ans = tmp[(cnt + 1) / 2];
    v[son[key]] -= ans;
    v[son[key ^ 1]] += ans;
    dfs(1);
    ans = abs(ans);
    for (int i = 1; i <= n; i++) ans += abs(f[i]);
    printf("%lld\n", ans);
  }
  return 0;
}
