#include <bits/stdc++.h>
using namespace std;
vector<int> g[101010];
int black_all = 0;
int white_all = 0;
long long ans = 0;
void dfs1(int v, int prev, int d) {
  if (d & 1)
    black_all++;
  else
    white_all++;
  for (auto u : g[v])
    if (u != prev) dfs1(u, v, d + 1);
}
pair<int, int> dfs2(int v, int prev, int d) {
  int black = 0, white = 0;
  for (auto u : g[v])
    if (u != prev) {
      pair<int, int> get = dfs2(u, v, d + 1);
      black += get.first;
      white += get.second;
    }
  if (d & 1)
    black++;
  else
    white++;
  ans += abs(black - white);
  return make_pair(black, white);
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  assert(n - 1 == m);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs1(0, -1, 0);
  if (black_all != white_all) {
    cout << -1 << endl;
    return 0;
  }
  dfs2(0, -1, 0);
  printf("%lld\n", ans);
  return 0;
}
