#include <bits/stdc++.h>
using namespace std;
int fa[500001], d[500001], f[500001], su, sv;
int hd[500001], nx[1000001], e[1000001], num;
int n, m, ans;
void dfs(int x, int dep) {
  d[x] = dep, f[x] = dep & 1 ? 1 : -1;
  for (int i = hd[x]; i; i = nx[i]) {
    if (e[i] == fa[x]) continue;
    if (d[e[i]]) {
      su = x, sv = e[i];
      continue;
    }
    fa[e[i]] = x;
    dfs(e[i], dep + 1);
    f[x] += f[e[i]];
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, x, y; i <= m; i++) {
    scanf("%d%d", &x, &y);
    nx[++num] = hd[x], hd[x] = num, e[num] = y;
    nx[++num] = hd[y], hd[y] = num, e[num] = x;
  }
  dfs(1, 1);
  if (m == n - 1) {
    if (f[1]) return (puts("-1"), 0);
    for (int i = 1; i <= n; i++) ans += abs(f[i]);
  } else if ((d[sv] - d[su]) & 1) {
    if (f[1]) return (puts("-1"), 0);
    vector<int> vec;
    while (sv != su) vec.push_back(f[sv]), d[sv] = 0, sv = fa[sv];
    sort(vec.begin(), vec.end());
    int mid = vec[(vec.size() + 1) / 2];
    ans = abs(mid);
    for (int i = 1; i <= n; i++)
      if (d[i]) ans += abs(f[i]);
    for (auto i : vec) ans += abs(i - mid);
  } else {
    if (f[1] & 1) return (puts("-1"), 0);
    int del = -f[1] / 2;
    ans = abs(del);
    while (su) f[su] += del, su = fa[su];
    while (sv) f[sv] += del, sv = fa[sv];
    for (int i = 1; i <= n; i++) ans += abs(f[i]);
  }
  printf("%d\n", ans);
  return 0;
}
