#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
vector<int> g[maxn];
int n, m;
int u, v, col[maxn], sum[maxn], up[maxn];
void dfs(int x, int fa) {
  col[x] = -col[fa];
  for (int i = 0; i < g[x].size(); i++) {
    int to = g[x][i];
    if (to == fa) continue;
    if (col[to]) {
      u = x, v = to;
      continue;
    }
    dfs(to, x);
  }
}
void dp(int x, int fa) {
  up[x] = fa;
  sum[x] = col[x];
  for (int i = 0; i < g[x].size(); i++) {
    int to = g[x][i];
    if (to == fa) continue;
    if ((to == u && x == v) || (to == v && x == u)) continue;
    dp(to, x);
    sum[x] += sum[to];
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  if (n & 1) {
    printf("-1\n");
    return 0;
  }
  col[0] = -1;
  dfs(1, 0);
  if (m == n - 1) {
    dp(1, 0);
    if (sum[1]) {
      printf("-1\n");
      return 0;
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) ans += abs(sum[i]);
    printf("%d\n", ans);
    return 0;
  }
  dp(u, 0);
  if (col[u] == col[v]) {
    int ans = abs(sum[u]) / 2;
    col[v] -= sum[u] / 2;
    col[u] -= sum[u] / 2;
    dp(u, 0);
    if (sum[u]) {
      printf("-1\n");
      return 0;
    }
    for (int i = 1; i <= n; i++) ans += abs(sum[i]);
    printf("%d\n", ans);
    return 0;
  }
  if (sum[u]) {
    printf("-1\n");
    return 0;
  }
  vector<int> all;
  all.clear();
  all.push_back(0);
  for (int i = v; i; i = up[i]) {
    all.push_back(sum[i]);
    sum[i] = 0;
  }
  sort(all.begin(), all.end());
  int x = all[all.size() / 2];
  int ans = 0;
  for (int i = 0; i < all.size(); i++) ans += abs(all[i] - x);
  for (int i = 1; i <= n; i++) ans += abs(sum[i]);
  printf("%d\n", ans);
  return 0;
}
