#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
vector<int> adj[N];
long long ans = 0;
int cnt[N][2];
void dfs(int u, int par, int dep) {
  bool cc = dep % 2;
  cnt[u][cc] = 1;
  cnt[u][!cc] = 0;
  for (int v : adj[u]) {
    if (v == par) continue;
    dfs(v, u, dep + 1);
    cnt[u][0] += cnt[v][0];
    cnt[u][1] += cnt[v][1];
  }
  ans += abs(cnt[u][0] - cnt[u][1]);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  if (m != n - 1) {
    return 0;
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1, 0, 0);
  if (cnt[1][0] == cnt[1][1])
    cout << ans << endl;
  else
    cout << -1 << endl;
}
