#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const long long mod = 1e9 + 7;
const int inf = 50000;
int ans, n, m, cnt, first[maxn], a[maxn], d[maxn], x, y, cut;
bool vis[maxn];
int f[maxn], tot, b[maxn];
struct Edge {
  int to, next;
} e[maxn << 1];
void add(int u, int v) {
  e[cnt] = (Edge){v, first[u]};
  first[u] = cnt++;
}
void Dfs2(int u, int fa) {
  for (int i = first[u]; i != -1; i = e[i].next) {
    if ((i ^ 1) == cut || cut == i) continue;
    int v = e[i].to;
    if (v == fa) continue;
    Dfs2(v, u);
    f[u] += f[v];
  }
}
void Dfs1(int u, int fa) {
  vis[u] = 1;
  d[u] = d[fa] + 1;
  a[u] = (d[u] & 1) ? 1 : -1;
  for (int i = first[u]; i != -1; i = e[i].next) {
    int v = e[i].to;
    if (v == fa) continue;
    if (vis[v]) {
      cut = i;
      x = u, y = v;
      continue;
    }
    Dfs1(v, u);
    a[u] += a[v];
  }
}
int main() {
  memset(first, -1, sizeof(first));
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    add(u, v);
    add(v, u);
  }
  Dfs1(1, 0);
  for (int i = 1; i <= n; i++) ans += abs(a[i]);
  if (m == n - 1) {
    if (a[1] != 0)
      cout << -1 << endl;
    else
      cout << ans << endl;
    return 0;
  }
  int len = d[x] + d[y] - 2 + 1;
  if (len & 1) {
    if (a[1] % 2 == 0)
      cout << ans + abs(a[1]) / 2 << endl;
    else
      cout << -1 << endl;
    return 0;
  }
  f[x] = 1, f[y] = -1;
  if (a[1] != 0) {
    cout << -1 << endl;
    return 0;
  }
  Dfs2(1, 0);
  ans = 0;
  for (int i = 1; i <= n; i++) {
    if (f[i] == 0)
      ans += abs(a[i]);
    else
      b[++tot] = -f[i] * a[i];
  }
  sort(b + 1, b + tot + 1);
  int val = b[(tot + 1) / 2];
  for (int i = 1; i <= tot; i++) ans += abs(val - b[i]);
  cout << ans + abs(val) << endl;
}
