#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5, M = 1e7 + 5, INF = 1e9, mod = 1e9 + 7;
const long long linf = 1e18;
const double eps = 1e-8, pi = acos(-1);
inline int gmin(int &a, int b) {
  if (a > b) a = b;
}
inline long long gmin(long long &a, long long b) {
  if (a > b) a = b;
}
inline int gmax(int &a, int b) {
  if (a < b) a = b;
}
inline long long gmax(long long &a, long long b) {
  if (a < b) a = b;
}
inline void read(long long &x) {
  long long f = 1, t = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    t = t * 10 + ch - '0';
    ch = getchar();
  }
  x = t * f;
}
inline long long read(long long &x, long long &y) {
  read(x);
  read(y);
}
inline void read(int &x) {
  int f = 1, t = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    t = t * 10 + ch - '0';
    ch = getchar();
  }
  x = t * f;
}
inline int read(int &x, int &y) {
  read(x);
  read(y);
}
inline void read(int &a, int &b, int &c) {
  read(a);
  read(b);
  read(c);
}
inline void read(long long &a, long long &b, long long &c) {
  read(a);
  read(b);
  read(c);
}
inline int read() {
  int x;
  read(x);
  return x;
}
inline long long qpow(long long a, long long b, long long p) {
  long long ret = 1;
  for (; b; b >>= 1, a = a * a % p)
    if (b & 1) ret = ret * a % p;
  return ret;
}
inline long long qpow(long long a, long long b) {
  long long ret = 1;
  for (; b; b >>= 1, a = a * a % mod)
    if (b & 1) ret = ret * a % mod;
  return ret;
}
inline long long qmul(long long a, long long b, long long p) {
  long long ret = 0;
  for (; b; b >>= 1, a = (a << 1) % p)
    if (b & 1) {
      ret = ret + a;
      if (ret >= p)
        ret -= p;
      else if (ret < 0)
        ret += p;
    };
  return ret;
}
inline long long qmul(long long a, long long b) {
  long long ret = 0;
  for (; b; b >>= 1, a = (a << 1) % mod)
    if (b & 1) {
      ret = ret + a;
      if (ret >= mod)
        ret -= mod;
      else if (ret < 0)
        ret += mod;
    };
  return ret;
}
inline void judge() {
  freopen("data.in", "r", stdin);
  freopen("data.out", "w", stdout);
}
inline void gen() { freopen("data.in", "w", stdout); }
struct edge {
  int t, ne;
} e[N];
int head[N], cnt, f[N], n, m, xx = 1, yy, fa[N], col[N], sum[N], inc[N];
int getf(int x) { return f[x] == x ? x : f[x] = getf(f[x]); }
void adde(int a, int b) {
  e[++cnt] = (edge){b, head[a]};
  head[a] = cnt;
}
void dfs(int x, int f) {
  sum[x] = col[x] = -col[f];
  fa[x] = f;
  for (int k = head[x]; k; k = e[k].ne)
    if (e[k].t != f) dfs(e[k].t, x), sum[x] += sum[e[k].t];
}
int main() {
  read(n, m);
  for (int i = (1); i <= (n); i++) f[i] = i;
  for (int i = (1); i <= (m); i++) {
    int x, y;
    read(x, y);
    if (getf(x) != getf(y))
      f[getf(x)] = getf(y);
    else {
      xx = x;
      yy = y;
      continue;
    }
    adde(x, y);
    adde(y, x);
  }
  col[0] = -1;
  dfs(xx, 0);
  long long ans = 0;
  if (yy == 0) {
    if (sum[xx]) return puts("-1"), 0;
    for (int i = (1); i <= (n); i++) ans += abs(sum[i]);
    return printf("%lld\n", ans), 0;
  }
  vector<int> cir;
  for (int i = yy; i; i = fa[i]) cir.push_back(sum[i]), inc[i] = 1;
  if (cir.size() & 1) {
    if (sum[xx] & 1) return puts("-1"), 0;
    for (int i = (1); i <= (n); i++)
      if (inc[i])
        ans += abs(sum[i] - sum[xx] / 2);
      else
        ans += abs(sum[i]);
    return printf("%lld\n", ans), 0;
  }
  if (sum[xx]) return puts("-1"), 0;
  sort(cir.begin(), cir.end());
  for (int i = (1); i <= (n); i++)
    if (inc[i])
      ans += abs(sum[i] - sum[cir[cir.size() / 2]]);
    else
      ans += abs(sum[i]);
  printf("%lld\n", ans);
}
