#include <bits/stdc++.h>
using namespace std;
const int N = 100050;
int n, m, a[N], sum[N], cnt[N], u, v, par[N], q[N];
vector<int> g[N];
long long solveTree() {
  int _ = 0;
  q[_++] = 1;
  for (int i = 0; i < (_); ++i) {
    int c = q[i];
    cnt[c] = 1;
    for (auto t : g[c])
      if (t != par[c]) par[q[_++] = t] = c, sum[t] = sum[c] ^ 1;
  }
  long long ans = 0;
  for (int i = _ - 1; i >= 0; --i) {
    int c = q[i];
    if (i) {
      ans += abs(sum[c] - (cnt[c] - sum[c]));
      int p = par[c];
      sum[p] += sum[c];
      cnt[p] += cnt[c];
    }
  }
  if (sum[1] * 2 != n) return -1;
  return ans;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < (m); ++i)
    scanf("%d%d", &u, &v), g[u].push_back(v), g[v].push_back(u);
  if (m == n - 1) printf("%lld\n", solveTree());
  return 0;
}
