#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
const int maxn = 100100;
struct edge {
  int id, nxt;
  edge() {}
  edge(int _id, int _nxt) : id(_id), nxt(_nxt) {}
};
edge e[(maxn << 1) + 5];
int st[maxn + 5], en = 0;
inline void add_edge(int first, int second) {
  e[en] = edge(second, st[first]), st[first] = en++;
}
int n;
int sum[maxn + 5][2];
int dep[maxn + 5];
long long ans;
void dfs(int first, int f = -1) {
  ans += dep[first];
  ++sum[first][dep[first] & 1];
  for (int i = st[first]; ~i; i = e[i].nxt) {
    int second = e[i].id;
    if (second == f) continue;
    dep[second] = dep[first] + 1;
    dfs(second, first);
    for (int j = (0), j_end_ = (2); j < j_end_; ++j)
      sum[first][j] += sum[second][j];
  }
  int tmp = min(sum[first][0], sum[first][1]);
  ans -= (long long)tmp * dep[first] << 1;
  for (int i = (0), i_end_ = (2); i < i_end_; ++i) sum[first][i] -= tmp;
}
int main() {
  memset(st, -1, sizeof st), en = 0;
  scanf("%d%*d", &n);
  if (n & 1) {
    printf("-1\n");
    return 0;
  }
  for (int i = (0), i_end_ = (n - 1); i < i_end_; ++i) {
    int u, v;
    scanf("%d%d", &u, &v), --u, --v;
    add_edge(u, v);
    add_edge(v, u);
  }
  ans = 0;
  dep[0] = 0;
  dfs(0);
  if (sum[0][0] || sum[0][1])
    puts("-1");
  else
    cout << ans << endl;
  return 0;
}
