#include <bits/stdc++.h>
using namespace std;
void read(int &x) {
  int r = 0, ne = 1;
  char c;
  while (c < '0' || c > '9') {
    if (c == '-') ne = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    r = r * 10 + c - '0';
    c = getchar();
  }
  x = r * ne;
}
struct Edge {
  int i, nxt;
};
int h[100002], tot;
Edge e[200002];
inline void _add(int x, int y) {
  e[++tot].i = y;
  e[tot].nxt = h[x];
  h[x] = tot;
  e[++tot].i = x;
  e[tot].nxt = h[y];
  h[y] = tot;
}
int n, m, x, y;
int s[100002], top, mid;
bool odd;
int A, B;
int w[100002];
int k[100002];
int sum;
long long ans;
void dfs(int x, int fa) {
  for (int i = h[x]; i; i = e[i].nxt) {
    int o = e[i].i;
    if (o == fa) continue;
    if (w[o]) {
      if (w[o] == w[x]) odd = true;
      A = x, B = o;
    } else {
      w[o] = -w[x];
      dfs(o, x);
    }
  }
}
void Dfs(int x, int fa) {
  for (int i = h[x]; i != 0; i = e[i].nxt) {
    int o = e[i].i;
    if (o == fa || (o == A && x == B) || (o == B && x == A)) continue;
    Dfs(o, x);
    w[x] += w[o];
    k[i] += k[o];
  }
}
signed main() {
  read(n), read(m);
  for (register int i = 0; i < m; ++i) {
    read(x), read(y);
    _add(x, y);
  }
  if (n % 2) {
    puts("-1");
    return 0;
  }
  w[1] = 1;
  dfs(1, 0);
  for (int i = 1; i <= n; ++i) sum += w[i];
  if (m + 1 == n) {
    if (sum) {
      puts("-1");
      return 0;
    }
  } else {
    if (odd) {
      if (sum & 1) {
        puts("-1");
        return 0;
      }
      ans += abs(sum >> 1);
      w[A] -= sum >> 1, w[B] -= sum >> 1;
    } else {
      if (sum) {
        puts("-1");
        return 0;
      } else
        k[A] = 1, k[B] = -1;
    }
  }
  Dfs(1, 0);
  for (register int i = 1; i <= n; ++i) {
    if (k[i])
      s[++top] = k[i] * w[i];
    else
      ans = ans + abs(w[i]);
  }
  s[++top] = 0;
  sort(s + 1, s + top + 1);
  mid = s[(top + 1) >> 1];
  for (register int i = 1; i <= top; ++i) ans += abs(s[i] - mid);
  printf("%lld\n", ans);
  return 0;
}
