#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chkmax(T &a, T b) {
  return (a < b) ? a = b, 1 : 0;
}
template <typename T>
bool chkmin(T &a, T b) {
  return (a > b) ? a = b, 1 : 0;
}
inline long long read() {
  long long x = 0, fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') fh = -1;
  for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ '0');
  return x * fh;
}
long long n, m;
bool gi;
long long he, to;
long long num[100005];
vector<long long> G[100005];
long long col[100005], vis[100005];
void dfs1(long long x, long long ff) {
  vis[x] = 1;
  for (long long v : G[x]) {
    if (v != ff) {
      if (!vis[v]) {
        col[v] = col[x] ^ 1;
        dfs1(v, x);
      } else {
        he = x, to = v;
        if (col[x] == col[v]) gi = 1;
      }
    }
  }
}
long long num2[100005];
long long sum;
long long ans;
void dfs2(long long x, long long ff) {
  for (long long v : G[x]) {
    if (v != ff && !(x == he && v == to || x == to && v == he)) {
      dfs2(v, x);
      num2[x] += num2[v];
      num[x] += num[v];
    }
  }
}
long long stk[100005], top;
signed main() {
  n = read();
  m = read();
  for (long long i = (1), _end_ = (long long)(m); i <= _end_; ++i) {
    long long x = read(), y = read();
    G[x].push_back(y);
    G[y].push_back(x);
  }
  col[1] = 1;
  dfs1(1, 0);
  for (long long i = (1), _end_ = (long long)(n); i <= _end_; ++i) {
    if (col[i])
      num[i] = 1;
    else
      num[i] = -1;
  }
  for (long long i = (1), _end_ = (long long)(n); i <= _end_; ++i)
    sum += num[i];
  if (m == n - 1) {
    if (sum) {
      puts("-1");
      return 0;
    }
  } else {
    if (gi) {
      if (sum % 2 == 1) {
        puts("-1");
        return 0;
      }
      num[he] -= (sum / 2);
      num[to] -= (sum / 2);
      ans += 1ll * abs(sum / 2);
    } else {
      if (sum) {
        puts("-1");
        return 0;
      }
      num2[he] = 1;
      num2[to] = -1;
    }
  }
  dfs2(1, 0);
  for (long long i = (1), _end_ = (long long)(n); i <= _end_; ++i) {
    if (!num2[i])
      ans += 1ll * abs(num[i]);
    else {
      if (num2[i]) num[i] = -num[i];
      stk[++top] = num[i];
    }
  }
  stk[++top] = 0;
  sort(stk + 1, stk + top + 1);
  long long X = stk[(top + 1) / 2];
  for (long long i = (1), _end_ = (long long)(top); i <= _end_; ++i)
    ans += 1ll * abs(stk[i] - X);
  printf("%lld\n", ans);
  return 0;
}
