#include <bits/stdc++.h>
using namespace std;
int h[100011];
int d[100011];
int can[100011];
vector<int> tab[100011];
long long ans = 0;
int maxHNode = 0;
int maxH = 0;
void dfs1(int now, int fa) {
  h[now] = 0;
  int sz = tab[now].size();
  int mx = 0, lmx = 0;
  for (int i = 0; i < sz; ++i) {
    int nex = tab[now][i];
    if (nex == fa) continue;
    dfs1(nex, now);
    if (h[nex] >= mx) {
      lmx = mx;
      mx = h[nex];
    } else if (h[nex] >= lmx) {
      lmx = h[nex];
    }
  }
  h[now] = lmx + 1;
  if (h[now] > maxH) {
    maxH = h[now];
    maxHNode = now;
  }
}
void dfs2(int now, int fa, int depth) {
  d[now] = depth;
  can[h[now]] += (d[now] & 1) ? -1 : 1;
  ans += d[now] * ((h[now] & 1) ? 1 : -1);
  int sz = tab[now].size();
  for (int i = 0; i < sz; ++i) {
    int nex = tab[now][i];
    if (nex == fa) continue;
    dfs2(nex, now, depth + 1);
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  if (m != n - 1) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 1; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    tab[a].push_back(b);
    tab[b].push_back(a);
  }
  dfs1(1, 1);
  dfs2(maxHNode, maxHNode, 0);
  for (int i = 1; i <= n; ++i) {
    if (can[i] != 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;
  return 0;
}
