#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void chkmax(T &a, T b) {
  if (a < b) a = b;
}
template <class T>
inline void chkmin(T &a, T b) {
  if (a > b) a = b;
}
inline int read() {
  int sum = 0, p = 1;
  char ch = getchar();
  while (!(('0' <= ch && ch <= '9') || ch == '-')) ch = getchar();
  if (ch == '-') p = -1, ch = getchar();
  while ('0' <= ch && ch <= '9') sum = sum * 10 + ch - 48, ch = getchar();
  return sum * p;
}
const int maxn = 1e5 + 20;
vector<int> to[maxn];
int n, m;
inline void init() {
  n = read();
  m = read();
  if (n & 1) puts("-1"), exit(0);
  for (int i = (1), _end_ = (m); i <= _end_; i++) {
    int u = read(), v = read();
    to[u].push_back(v);
    to[v].push_back(u);
  }
}
pair<long long, long long> f[maxn];
namespace p1 {
void dfs(int u, int fa) {
  for (int v : to[u]) {
    if (v == fa) continue;
    dfs(v, u);
    f[u].first += f[v].first + abs(f[v].second);
    f[u].second += f[v].second;
  }
  f[u].second = -f[u].second + 1;
}
inline void doing() {
  dfs(1, 0);
  if (f[1].second)
    puts("-1");
  else
    printf("%lld\n", f[1].first);
}
};  // namespace p1
namespace p2 {
int cir[maxn], len;
int vis[maxn];
int cx, cy;
void dfs(int u, int fa) {
  vis[u] = 1;
  for (int v : to[u]) {
    if (v == fa) continue;
    if (vis[v]) {
      cx = u, cy = v;
      continue;
    }
    dfs(v, u);
  }
}
void find_cir(int u, int fa) {
  cir[++len] = u;
  if (cir[len] == cy) return;
  for (int v : to[u]) {
    if (v == fa) continue;
    find_cir(v, u);
    if (cir[len] == cy) return;
  }
  --len;
}
void dfs1(int u, int fa) {
  for (int v : to[u]) {
    if (v == fa || vis[v]) continue;
    dfs1(v, u);
    f[u].first += f[v].first + abs(f[v].second);
    f[u].second += f[v].second;
  }
  f[u].second = -f[u].second + 1;
}
int st[maxn], x[maxn];
inline void doing() {
  dfs(1, 0);
  to[cx].erase(find(to[cx].begin(), to[cx].end(), cy));
  to[cy].erase(find(to[cy].begin(), to[cy].end(), cx));
  find_cir(cx, 0);
  for (int i = (1), _end_ = (n); i <= _end_; i++) vis[i] = 0;
  for (int i = (1), _end_ = (len); i <= _end_; i++) vis[cir[i]] = 1;
  long long ans = 0;
  for (int i = (1), _end_ = (len); i <= _end_; i++)
    dfs1(cir[i], 0), ans += f[i].first;
  for (int i = (1), _end_ = (len); i <= _end_; i++)
    st[i] = f[i].second - (f[i - 1 ? i - 1 : n].second);
  if (len & 1) {
    long long t = 0, sum = 0, now = 1;
    for (int i = (1), _end_ = (len); i <= _end_; i++) {
      x[++t] = sum;
      if ((now += 2) > len) now -= len;
      sum += st[now];
    }
    long long a1 = 0;
    assert(sum == 0);
    sort(x + 1, x + t + 1);
    for (int i = (1), _end_ = (len); i <= _end_; i++) a1 -= x[i];
    a1 /= len;
    for (int i = (1), _end_ = (len); i <= _end_; i++) x[i] += a1;
    for (int i = (1), _end_ = (len); i <= _end_; i++) ans += abs(x[i]);
    printf("%lld\n", ans);
  } else {
    long long sum1 = 0, sum2 = 0;
    for (int i = (1), _end_ = (len); i <= _end_; i++)
      if (i & 1)
        sum1 += f[i].second;
      else
        sum2 += f[i].second;
    if (!(sum1 == sum2)) puts("-1"), exit(0);
    x[1] = 0;
    for (int i = (2), _end_ = (len); i <= _end_; i++) {
      x[i] = -x[i - 1] + f[i].second;
    }
    for (int i = (3), _end_ = (len); i <= _end_; i++)
      if (i & 1) x[i] = -x[i];
    sort(x + 1, x + len + 1);
    for (int i = (1), _end_ = (len); i <= _end_; i++)
      ans += abs(x[i] - x[len + 1 >> 1]);
    printf("%lld\n", ans);
  }
}
};  // namespace p2
inline void doing() {
  if (m == n - 1)
    p1::doing();
  else
    p2::doing();
}
int main() {
  init();
  doing();
  return 0;
}
