#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
const int mod = 1e9 + 7;
const int MAX = 1000000010;
const double eps = 1e-6;
const double PI = acos(-1);
template <typename T>
inline void read(T &x) {
  x = 0;
  T f = 1;
  char ch;
  do {
    ch = getchar();
    if (ch == '-') f = -1;
  } while (ch < '0' || ch > '9');
  do x = x * 10 + ch - '0', ch = getchar();
  while (ch <= '9' && ch >= '0');
  x *= f;
}
template <typename A, typename B>
inline void read(A &x, B &y) {
  read(x);
  read(y);
}
template <typename A, typename B, typename C>
inline void read(A &x, B &y, C &z) {
  read(x);
  read(y);
  read(z);
}
template <typename A, typename B, typename C, typename D>
inline void read(A &x, B &y, C &z, D &w) {
  read(x);
  read(y);
  read(z);
  read(w);
}
template <typename A, typename B>
inline A fexp(A x, B p) {
  A ans = 1;
  for (; p; p >>= 1, x = 1LL * x * x % mod)
    if (p & 1) ans = 1LL * ans * x % mod;
  return ans;
}
template <typename A, typename B>
inline A fexp(A x, B p, A mo) {
  A ans = 1;
  for (; p; p >>= 1, x = 1LL * x * x % mo)
    if (p & 1) ans = 1LL * ans * x % mo;
  return ans;
}
int n, m;
namespace Tree {
vector<int> G[maxn];
int V[maxn];
void dfs(int u, int f, int dep) {
  if (dep & 1)
    V[u] = 1;
  else
    V[u] = -1;
  for (auto v : G[u])
    if (v != f) {
      dfs(v, u, dep + 1);
      V[u] += V[v];
    }
}
void solve() {
  for (long long i = (2); i <= (n); i++) {
    int u, v;
    read(u, v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(1, 0, 1);
  if (V[1]) {
    puts("-1");
  } else {
    long long ans = 0;
    for (long long i = (1); i <= (n); i++) ans += abs(V[i]);
    cout << ans << endl;
  }
}
};  // namespace Tree
namespace Circle {
int U[maxn], V[maxn];
vector<int> G[maxn];
int col[maxn];
int fa[maxn];
int find(int i) { return fa[i] == i ? i : fa[i] = find(fa[i]); }
void dfs(int u, int c) {
  if (col[u]) return;
  col[u] = c;
  for (auto v : G[u]) dfs(v, -c);
}
namespace Odd {
int f[maxn];
void dfs(int u, int fa) {
  f[u] = col[u];
  for (auto v : G[u])
    if (v != fa) {
      dfs(v, u);
      f[u] += f[v];
    }
}
void solve() {
  int tot = 0;
  for (long long i = (1); i <= (n); i++) tot += col[i];
  if (tot & 1) {
    puts("-1");
  } else {
    col[U[m]] -= tot / 2;
    col[V[m]] -= tot / 2;
    dfs(1, 0);
    long long ans = 0;
    for (long long i = (1); i <= (n); i++) ans += abs(f[i]);
    cout << ans + abs(tot / 2) << endl;
  }
}
}  // namespace Odd
namespace Even {
pair<int, int> f[maxn];
int has[maxn];
void dfs(int u, int fa) {
  f[u].first = col[u];
  if (u == U[n]) f[u].second = 1;
  if (u == V[n]) f[u].second = -1;
  for (auto v : G[u])
    if (v != fa) {
      dfs(v, u);
      f[u].first += f[v].first;
      f[u].second += f[v].second;
    }
}
void solve() {
  int tot = 0;
  for (long long i = (1); i <= (n); i++) tot += col[i];
  if (tot) {
    puts("-1");
  } else {
    dfs(1, 0);
    long long ans = 0;
    int cnt = 0;
    for (long long i = (1); i <= (n); i++)
      if (!f[i].second)
        ans += f[i].first;
      else
        has[++cnt] = f[i].second * f[i].first;
    has[++cnt] = 0;
    sort(has + 1, has + cnt + 1);
    for (long long i = (1); i <= (cnt); i++)
      ans += abs(has[i] - has[(n + 1) / 2]);
    cout << ans << endl;
  }
}
}  // namespace Even
void solve() {
  for (long long i = (1); i <= (n); i++) read(U[i], V[i]);
  for (long long i = (1); i <= (n); i++) fa[i] = i;
  for (long long i = (1); i <= (m); i++) {
    if (find(U[i]) == find(V[i])) {
      swap(U[i], U[m]);
      swap(V[i], V[m]);
      break;
    } else
      fa[find(U[i])] = find(V[i]);
  }
  for (long long i = (1); i <= (n - 1); i++)
    G[U[i]].push_back(V[i]), G[V[i]].push_back(U[i]);
  dfs(1, 1);
  if (col[U[m]] == col[V[m]])
    Odd::solve();
  else
    Even::solve();
}
}  // namespace Circle
void Work() {
  if (n == m + 1)
    Tree::solve();
  else
    Circle::solve();
}
void Init() { read(n, m); }
int main() {
  Init();
  Work();
  return 0;
}
