#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
vector<int> Mat[MAXN];
int n, cnt[MAXN][2], m;
long long ans;
void dfs(int v, int p, int h) {
  for (int i = 0; i < Mat[v].size(); i++)
    if (Mat[v][i] != p) {
      dfs(Mat[v][i], v, h + 1);
      cnt[v][0] += cnt[Mat[v][i]][0];
      cnt[v][1] += cnt[Mat[v][i]][1];
      ans += abs(cnt[Mat[v][i]][0] - cnt[Mat[v][i]][1]);
    }
  cnt[v][h % 2]++;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    Mat[a].push_back(b);
    Mat[b].push_back(a);
  }
  dfs(1, 1, 0);
  if (cnt[1][0] != cnt[1][1]) return cout << -1, 0;
  cout << ans;
}
