#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005, maxe = maxn << 1;
int n, m, fa[maxn], U, V, flg, w[maxn], s[maxn], Q[maxn];
int e, son[maxe], nxt[maxe], lnk[maxn];
long long ans;
inline char nc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline int read() {
  int ret = 0;
  bool f = 0;
  char ch = nc();
  while (ch > '9' || ch < '0') f ^= ch == '-', ch = nc();
  while (ch <= '9' && ch >= '0') ret = ret * 10 + ch - '0', ch = nc();
  return f ? -ret : ret;
}
int getfa(int x) { return fa[x] == x ? x : fa[x] = getfa(fa[x]); }
inline void add_e(int x, int y) {
  son[++e] = y;
  nxt[e] = lnk[x];
  lnk[x] = e;
}
inline int abs(int x) { return x < 0 ? -x : x; }
inline long long solve(int x) {
  w[U] += x, w[V] += x * flg;
  memset(s, 0, sizeof s);
  ans = 0;
  for (int i = n; i; i--)
    s[Q[i]] += w[Q[i]], ans += abs(s[Q[i]]), s[fa[Q[i]]] += s[Q[i]];
  w[U] -= x, w[V] -= x * flg;
  return s[1] == 0 ? ans + abs(x) : 1e18;
}
int main() {
  n = read(), m = read();
  if (n & 1) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int u = read(), v = read();
    int fu = getfa(u), fv = getfa(v);
    if (fu == fv)
      U = u, V = v;
    else
      add_e(u, v), add_e(v, u), fa[fu] = fv;
  }
  int hed = 0, til = 1;
  Q[1] = fa[1] = 1;
  w[1] = 1;
  while (hed ^ til)
    for (int i = lnk[Q[++hed]]; i; i = nxt[i])
      if (son[i] ^ fa[Q[hed]])
        Q[++til] = son[i], fa[son[i]] = Q[hed], w[son[i]] = -w[Q[hed]];
  if (U) {
    flg = w[U] == w[V] ? 1 : -1;
    int L = -n, R = n;
    while (L < R) {
      int midl = (L + L + R) / 3, midr = (L + R + R + 2) / 3;
      if (solve(midl) < solve(midr))
        R = midr - 1;
      else
        L = midl + 1;
    }
    ans = solve(L);
  } else
    ans = solve(0);
  printf("%lld\n", ans == 1e18 ? -1 : ans);
  return 0;
}
