#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
struct edge {
  int s, t, n;
} e[2 * N];
int n, m, len, h[N], s[N], col[N], fa[N], ufs[N], inC[N], a[N];
long long ans;
int find(int x) { return ufs[x] == x ? x : ufs[x] = find(ufs[x]); }
void dfs(int x, int f) {
  col[x] = -col[f], s[x] = col[x], fa[x] = f;
  for (int i = h[x], y; y = e[i].t, i; i = e[i].n)
    if (y != f) dfs(y, x), s[x] += s[y];
}
void work() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) ufs[i] = i;
  int x = 1, y;
  for (int i = 1, u, v, tot = 0; i <= m; i++) {
    scanf("%d %d", &u, &v);
    if (find(u) != find(v)) {
      e[++tot] = (edge){u, v, h[u]}, h[u] = tot;
      e[++tot] = (edge){v, u, h[v]}, h[v] = tot;
      ufs[find(u)] = find(v);
    } else
      x = u, y = v;
  }
  col[0] = -1, dfs(x, 0);
  if (m == n - 1) {
    if (s[1]) puts("-1"), exit(0);
    for (int i = 1; i <= n; i++) ans += fabs(s[i]);
  } else {
    for (int i = y; i; inC[i] ^= 1, a[++len] = s[i], i = fa[i])
      ;
    for (int i = x; i; inC[i] ^= 1, a[++len] = s[i], i = fa[i])
      ;
    if (len & 1) {
      if (s[x] & 1) puts("-1"), exit(0);
      for (int i = 1; i <= n; i++)
        if (inC[i])
          ans += fabs(s[i] - s[x] / 2);
        else
          ans += fabs(s[i]);
    } else {
      if (s[x]) puts("-1"), exit(0);
      sort(a + 1, a + len + 1);
      for (int i = 1; i <= n; i++)
        if (inC[i])
          ans += fabs(s[i] - a[len / 2]);
        else
          ans += fabs(s[i]);
    }
  }
  printf("%lld", ans);
}
int main() {
  work();
  return 0;
}
