#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
constexpr int inf = 1000000010;
constexpr long long INF = 1000000000000000010;
constexpr long double eps = 1e-12;
constexpr long double pi = 3.141592653589793238;
template <class T, class U>
inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U>
inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int N = 200010;
vector<vector<int>> graph(N, vector<int>());
vector<int> col(N), cnt(N);
void dfs1(int n, int p, int c) {
  col[n] = c;
  for (int i : graph[n]) {
    if (i == p) continue;
    dfs1(i, n, c * -1);
  }
}
void dfs2(int n, int p) {
  int res = col[n];
  for (int i : graph[n]) {
    if (i == p) continue;
    dfs2(i, n);
    res += cnt[i];
  }
  cnt[n] = res;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int n, m;
  cin >> n >> m;
  assert(m == n - 1);
  for (long long i = 0; i < (m); i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    graph[u].push_back(v);
    graph[v].push_back(u);
  }
  dfs1(0, -1, 1);
  dfs2(0, -1);
  if (cnt[0] != 0) {
    cout << "-1\n";
    return 0;
  }
  long long ans = 0;
  for (long long i = 0; i < (n); i++) ans += abs(cnt[i]);
  cout << ans << '\n';
}
