#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, m, w[N], head[N], nxt[N];
void add(int f, int t) {
  static int cnt = 0;
  w[++cnt] = t;
  nxt[cnt] = head[f];
  head[f] = cnt;
}
int c1[N], c2[N];
long long dp[N];
void dfs(int x, int h, int fa) {
  (h & 1 ? c1[x] : c2[x]) = 1;
  for (int i = head[x]; i; i = nxt[i])
    if (w[i] != fa) {
      int v = w[i];
      dfs(v, h + 1, x);
      dp[x] += dp[v] + abs(c1[v] - c2[v]);
      c1[x] += c1[v];
      c2[x] += c2[v];
    }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  dfs(1, 0, 0);
  printf("%lld\n", c1[1] == c2[1] ? dp[1] : -1ll);
  return 0;
}
