#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m, cnt, root = 1, rt = 1, ans, fa[N], head[N], a[N], f[N];
struct edge {
  int to, from;
} e[N * 2];
void insert(int x, int y) {
  e[++cnt].to = y, e[cnt].from = head[x], head[x] = cnt;
  e[++cnt].to = x, e[cnt].from = head[y], head[y] = cnt;
}
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void dfs(int x, int y) {
  a[x] = y;
  for (int i = head[x]; i; i = e[i].from)
    if (e[i].to != fa[x]) fa[e[i].to] = x, dfs(e[i].to, -y), a[x] += a[e[i].to];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1, x, y; i <= m; i++) {
    scanf("%d%d", &x, &y);
    if (find(x) != find(y))
      fa[fa[x]] = fa[y];
    else {
      root = x, rt = y;
      continue;
    }
    insert(x, y);
  }
  fa[root] = 0, dfs(root, 1);
  if (n == m) {
    for (int i = rt; i; i = fa[i]) f[++cnt] = a[i];
    if (cnt & 1) {
      if (a[root] & 1) {
        printf("-1");
        return 0;
      }
      for (int i = rt; i; i = fa[i]) a[i] -= a[root] / 2;
    } else {
      if (a[root]) {
        printf("-1");
        return 0;
      }
      sort(f + 1, f + cnt + 1);
      for (int i = rt; i; i = fa[i]) a[i] -= f[cnt / 2];
    }
  } else if (a[root]) {
    printf("-1");
    return 0;
  }
  for (int i = 1; i <= n; i++) ans += abs(a[i]);
  printf("%d", ans);
}
