#include <bits/stdc++.h>
using namespace std;
const int N = 200000;
int n, m;
vector<int> bi[N];
int ans;
int dfs(int t, int f = 0, int d = 1) {
  int c0 = 0, c1 = 0;
  if (d & 1)
    c1++;
  else
    c0++;
  for (int i = 0; i < bi[t].size(); ++i)
    if (bi[t][i] != f) {
      int x = dfs(bi[t][i], t, d + 1);
      if (x < 0)
        c0 -= x;
      else
        c1 += x;
    }
  ans -= 2 * min(c0, c1) * d;
  ans += d;
  return c1 - c0;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    bi[a].push_back(b), bi[b].push_back(a);
  }
  if (n == m) return 0;
  int x = dfs(1);
  if (x) return puts("-1"), 0;
  cout << ans << endl;
}
