#include <bits/stdc++.h>
using namespace std;
const int NMAX = 100005;
vector<int> G[NMAX];
int treeSize[NMAX], black[NMAX];
inline int64_t mabs(int64_t x) { return x < 0 ? -x : x; }
int64_t ans = 0;
void dfs(int node, int prev, int val = 0) {
  treeSize[node] = 1;
  black[node] = val;
  for (int to : G[node]) {
    if (to != prev) {
      dfs(to, node, val ^ 1);
      treeSize[node] += treeSize[to];
      black[node] += black[to];
    }
  }
  ans += mabs(treeSize[node] - 2 * black[node]);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dfs(1, -1);
  if (treeSize[1] - black[1] == black[1]) {
    cout << ans << '\n';
  } else {
    cout << "-1\n";
  }
}
