#include <bits/stdc++.h>
using namespace std;
inline int Read() {
  int x = 0, f = 1, c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
const int MAXN = 100005;
int n, m, rta = 1, rtb, f[MAXN], siz[MAXN], sic[MAXN];
bool cir[MAXN], col[MAXN];
vector<int> adj[MAXN];
long long ans;
inline int Find(int x) {
  while (x ^ f[x]) x = f[x] = f[f[x]];
  return x;
}
inline void Dfs(int x, int p) {
  sic[x] = col[x], siz[x] = 1, cir[x] = x == rtb;
  for (auto y : adj[x])
    if (y ^ p)
      col[y] = col[x] ^ 1, Dfs(y, x), siz[x] += siz[y], sic[x] += sic[y],
      cir[x] |= cir[y];
}
int main() {
  n = Read(), m = Read();
  if (n & 1) return puts("-1"), 0;
  for (int i = 1; i <= n; i++) f[i] = i;
  for (int i = 1, x, y; i <= m; i++) {
    x = Read(), y = Read();
    if (Find(x) ^ Find(y))
      f[Find(x)] = Find(y), adj[x].push_back(y), adj[y].push_back(x);
    else
      rta = x, rtb = y;
  }
  Dfs(rta, 0);
  if (m == n - 1) {
    if (sic[1] << 1 ^ n) return puts("-1"), 0;
    for (int i = 1; i <= n; i++) ans += abs(siz[i] - sic[i] - sic[i]);
    return printf("%lld\n", ans), 0;
  }
  if (col[rtb]) {
    if (sic[rta] << 1 ^ n) return puts("-1"), 0;
    vector<int> tmp;
    for (int i = 1; i <= n; i++)
      if (cir[i])
        tmp.push_back(siz[i] - sic[i] - sic[i]);
      else
        ans += abs(siz[i] - sic[i] - sic[i]);
    sort(tmp.begin(), tmp.end());
    for (auto x : tmp) ans += abs(x - tmp[tmp.size() / 2]);
    return printf("%lld\n", ans), 0;
  }
  int c = siz[rta] - (n >> 1);
  ans = abs(c);
  for (int i = 1; i <= n; i++)
    if (i ^ rta)
      if (cir[i])
        ans += abs(siz[i] - sic[i] - sic[i] + c);
      else
        ans += abs(siz[i] - sic[i] - sic[i]);
  return printf("%lld\n", ans), 0;
}
