#include <bits/stdc++.h>
using namespace std;
long long ans = 0;
vector<int> graph[100005];
int depth[100005];
int parent[100005];
int odd_cnt[100005], even_cnt[100005];
void dfs(int node, int prt) {
  depth[node] = depth[prt] + 1;
  parent[node] = prt;
  for (int i = 0; i < graph[node].size(); i++) {
    int nxt = graph[node][i];
    if (nxt == prt) continue;
    dfs(nxt, node);
    odd_cnt[node] += odd_cnt[nxt];
    even_cnt[node] += even_cnt[nxt];
  }
  if (depth[node] % 2) {
    odd_cnt[node]++;
  } else {
    even_cnt[node]++;
  }
  if (node != 1)
    ans += (long long)(max(odd_cnt[node], even_cnt[node]) -
                       min(odd_cnt[node], even_cnt[node]));
}
void inputtree(int siz, int root = 1) {
  for (int i = 1; i < siz; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
  dfs(root, 0);
}
int n, m;
int main() {
  scanf("%d%d", &n, &m);
  inputtree(n);
  if (odd_cnt[1] != even_cnt[1]) {
    printf("-1");
  } else {
    printf("%lld", ans);
  }
  return 0;
}
