#include <bits/stdc++.h>
using namespace std;
inline long long myabs(long long x) { return x < 0 ? -x : x; }
int n, m;
vector<int> G[100010];
int dep[100010];
long long k[100010], b[100010];
long long a[100010];
int tot;
long long ans;
bool odd;
int u, v;
void prework(int x, int p) {
  for (int i = 0; i < (int)G[x].size(); i++) {
    int y = G[x][i];
    if (y == p) continue;
    if (dep[y]) {
      if (dep[y] < dep[x]) u = x, v = y;
      continue;
    }
    dep[y] = dep[x] + 1;
    prework(y, x);
  }
}
void dfs(int x, int p) {
  for (int i = 0; i < (int)G[x].size(); i++) {
    int y = G[x][i];
    if (y == p || (x == u && y == v) || (x == v && y == u)) continue;
    dfs(y, x);
    k[x] += k[y];
    b[x] += b[y];
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dep[1] = 1;
  prework(1, 0);
  long long num = 0;
  for (int i = 1; i <= n; i++)
    if (dep[i] & 1)
      b[i] = 1;
    else
      b[i] = -1;
  for (int i = 1; i <= n; i++) num += b[i];
  if (n - 1 == m) {
    if (num) {
      printf("-1");
      return 0;
    }
  } else {
    if (~(dep[u] - dep[v]) & 1) {
      if (num % 2 == 1) {
        printf("-1");
        return 0;
      }
      b[u] -= num / 2;
      b[v] -= num / 2;
      ans += myabs(num / 2);
    } else {
      if (num) {
        printf("-1");
        return 0;
      }
      k[u] = 1;
      k[v] = -1;
    }
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++) {
    if (!k[i])
      ans += myabs(b[i]);
    else
      a[++tot] = b[i];
  }
  sort(a + 1, a + tot + 1);
  long long x = a[(tot + 1) / 2];
  if (tot) ans = myabs(x);
  for (int i = 1; i <= tot; i++) ans += myabs(a[i] - x);
  printf("%lld", ans);
  return 0;
}
