#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
struct edge {
  int v;
  edge *next;
} pool[3 * N], *h[N];
int n, m, top = -1;
int size[N], ans;
inline void addedge(int u, int v) {
  edge *tmp = &pool[++top];
  tmp->v = v;
  tmp->next = h[u];
  h[u] = tmp;
}
void dfs(int u, int pre, int res) {
  for (edge *tmp = h[u]; tmp; tmp = tmp->next) {
    int v = tmp->v;
    if (v == pre) continue;
    dfs(v, u, -res);
    size[u] += size[v];
  }
  size[u] += res;
  if (pre) ans += abs(size[u]);
}
void init() {
  int x, y;
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &x, &y);
    addedge(x, y);
    addedge(y, x);
  }
  dfs(1, 0, 1);
  if (!size[1])
    cout << ans << endl;
  else
    cout << -1 << endl;
}
int main() {
  cin >> n >> m;
  if (m == n) return 1;
  init();
  return 0;
}
