#include <bits/stdc++.h>
using namespace std;
int n, m;
int f[100005], fa[100005], anc[100005], dep[100005], s, t;
vector<int> g[100005];
int read() {
  char c = getchar();
  int x = 0;
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return x;
}
int find(int x) { return anc[x] == x ? x : anc[x] = find(anc[x]); }
void DFS(int u, int pre, int type) {
  f[u] = type;
  fa[u] = pre;
  dep[u] = dep[pre] + 1;
  for (int i = 0; i < (int)g[u].size(); ++i) {
    int v = g[u][i];
    if (v == pre) continue;
    DFS(v, u, -type);
    f[u] += f[v];
  }
}
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; ++i) anc[i] = i;
  for (int i = 1; i <= m; ++i) {
    int u = read(), v = read();
    if (find(u) == find(v)) {
      s = u;
      t = v;
      continue;
    }
    anc[find(u)] = find(v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  DFS(s, 0, 1);
  if (m == n - 1) {
    if (f[s] != 0) {
      puts("-1");
      return 0;
    }
    int res = 0;
    for (int i = 1; i <= n; ++i) res += abs(f[i]);
    printf("%d\n", res);
  } else if (dep[t] & 1) {
    int minus = f[s], res = 0;
    if (minus & 1) {
      puts("-1");
      return 0;
    }
    res += abs(minus >> 1);
    f[s] = 0;
    for (int i = t; i != s; i = fa[i]) f[i] -= (minus >> 1);
    for (int i = 1; i <= n; ++i) res += abs(f[i]);
    printf("%d\n", res);
  } else {
    vector<int> circle;
    for (int i = t; i != s; i = fa[i]) {
      circle.push_back(f[i]);
      f[i] = 0;
    }
    circle.push_back(0);
    sort(circle.begin(), circle.end());
    int v = circle[((circle.size() + 1) >> 1) - 1], res = 0;
    for (int i = 0; i < (int)circle.size(); ++i) res += abs(circle[i] - v);
    for (int i = 1; i <= n; ++i) res += abs(f[i]);
    printf("%d\n", res);
  }
}
