#include <bits/stdc++.h>
using std::cerr;
using std::endl;
inline int rd() {
  int x;
  scanf("%d", &x);
  return x;
}
const int N = 1e5 + 233;
int n, m, val[N], size;
std::vector<int> G[N];
int sum[N];
int stk[N], top;
long long ans;
std::vector<int> cir;
void dfs1(int x, int fa, int c) {
  val[x] = sum[x] = c;
  stk[++top] = x;
  for (int y : G[x])
    if (y != fa) {
      if (val[y]) {
        if (cir.size()) continue;
        for (int z; y != z; --top) {
          z = stk[top];
          cir.push_back(z);
        }
      } else {
        dfs1(y, x, -c);
        sum[x] += sum[y];
      }
    }
}
int main() {
  n = rd(), m = rd();
  if (n & 1) return puts("-1"), 0;
  for (int i = 1; i <= m; ++i) {
    int x = rd(), y = rd();
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dfs1(1, 0, 1);
  if (m == n - 1) {
    if (sum[1]) return puts("-1"), 0;
  } else {
    size = cir.size();
    if (size & 1) {
      if (sum[1] & 1) return puts("0"), 0;
      int v = sum[1] >> 1;
      for (int x : cir) sum[x] -= v;
    } else {
      if (sum[1]) return puts("0"), 0;
      std::sort(cir.begin(), cir.end(),
                [&](int x, int y) { return sum[x] < sum[y]; });
      int v = sum[cir[size / 2 - 1]];
      for (int x : cir) sum[x] -= v;
    }
  }
  for (int i = 1; i <= n; ++i) ans += std::abs(sum[i]);
  std::cout << ans << std::endl;
  return 0;
}
