#include <bits/stdc++.h>
using namespace std;
const long long INF = 4e18;
const int BASE = 131;
const double eps = 1e-8;
const int mod = 998244353;
const int inf = 1061109567;
const double pi = acos(-1);
const int inv2 = (mod + 1) >> 1;
namespace Math {
inline long long popcount(long long x) {
  x = (x & 0x55555555) + ((x >> 1) & 0x55555555);
  x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
  x = (x & 0x0F0F0F0F) + ((x >> 4) & 0x0F0F0F0F);
  x = (x & 0x00FF00FF) + ((x >> 8) & 0x00FF00FF);
  x = (x & 0x0000FFFF) + ((x >> 16) & 0x0000FFFF);
  return x;
}
inline long long F(long long x, long long p) { return x >= p ? x - p : x; }
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
inline long long ksm(long long x, long long t, long long p) {
  long long res = 1;
  while (t) {
    if (t & 1) res = 1LL * res * x % p;
    x = 1LL * x * x % p;
    t >>= 1;
  }
  return res;
}
}  // namespace Math
using Math::F;
using Math::ksm;
using Math::popcount;
template <typename tp>
inline void read(tp& dig) {
  char ch = getchar();
  long long flag = 0;
  dig = 0;
  while (!isdigit(ch)) {
    if (ch == '-') flag = 1;
    ch = getchar();
  }
  while (isdigit(ch)) dig = dig * 10 + ch - '0', ch = getchar();
  if (flag) dig = -dig;
}
template <typename tp, typename... Args>
inline void read(tp& dig, Args&... args) {
  read(dig);
  read(args...);
}
int n, m, X, Y, lnum, flag, vis[200010], stk[200010];
int fst[200010], nxt[200010], to[200010];
long long ans, a[200010], k[200010], b[200010];
inline void add_edge(int u, int v) {
  nxt[++lnum] = fst[u];
  fst[u] = lnum;
  to[lnum] = v;
  nxt[++lnum] = fst[v];
  fst[v] = lnum;
  to[lnum] = u;
}
inline void dfs(int u, int fa) {
  a[u] = a[fa] ^ 1;
  vis[u] = 1;
  for (int i = fst[u]; i; i = nxt[i]) {
    if (to[i] == fa) continue;
    if (vis[to[i]]) {
      if (a[u] ^ a[to[i]]) flag = 1;
      X = u, Y = to[i];
      continue;
    }
    dfs(to[i], u);
  }
}
inline void Dfs(int u, int fa) {
  for (int i = fst[u]; i; i = nxt[i]) {
    if (to[i] == fa || (X == u && Y == to[i]) || (X == to[i] && Y == u))
      continue;
    Dfs(to[i], u);
    k[u] += k[to[i]];
    b[u] += b[to[i]];
  }
}
int main() {
  read(n);
  read(m);
  if (n & 1) {
    printf("-1\n");
    return 0;
  }
  for (int i = 1, x, y; i <= m; ++i) {
    read(x);
    read(y);
    add_edge(x, y);
  }
  dfs(1, 0);
  int tot = 0;
  for (int i = 1; i <= n; ++i) b[i] = (a[i] ? 1 : -1), tot += b[i];
  if (m < n) {
    if (tot) {
      printf("-1\n");
      return 0;
    }
  } else {
    if (!flag) {
      if (tot & 1) {
        printf("-1\n");
        return 0;
      }
      b[X] -= tot >> 1, b[Y] -= tot >> 1;
      ans += abs(tot >> 1);
    } else {
      if (tot) {
        printf("-1\n");
        return 0;
      }
      k[X] = 1, k[Y] = -1;
    }
  }
  Dfs(1, 0);
  for (int i = 1; i <= n; ++i)
    if (!k[i])
      ans += abs(b[i]);
    else
      stk[++stk[0]] = b[i];
  stk[++stk[0]] = 0;
  sort(stk + 1, stk + stk[0] + 1);
  for (int i = 1; i <= stk[0]; ++i) ans += abs(stk[i] - stk[stk[0] + 1 >> 1]);
  printf("%lld\n", ans);
  return 0;
}
