#include <bits/stdc++.h>
using namespace std;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }
const int inf = (int)1.01e9;
const long double eps = 1e-9;
const long double pi = acos((long double)-1.0);
const int mod = (int)1e9 + 7;
void add(int &x, int y) {
  if ((x += y) >= mod) {
    x -= mod;
  }
}
int mult(int x, int y) { return (long long)x * y % mod; }
int myPower(int x, int pw) {
  int res = 1;
  for (; pw; pw >>= 1) {
    if (pw & 1) {
      res = mult(res, x);
    }
    x = mult(x, x);
  }
  return res;
}
void precalc() {}
const int maxn = (int)1e5 + 10;
int n, m;
vector<vector<int> > es;
int read() {
  if (scanf("%d%d", &n, &m) < 2) {
    return 0;
  }
  es = vector<vector<int> >(n);
  for (int i = 0; i < m; ++i) {
    int s, t;
    scanf("%d%d", &s, &t);
    --s, --t;
    es[s].push_back(t), es[t].push_back(s);
  }
  return 1;
}
long long res;
int dfs(int v, int p) {
  int sum = 1;
  for (int u : es[v]) {
    if (u == p) {
      continue;
    }
    int got = dfs(u, v);
    res += abs(got);
    sum -= got;
  }
  return sum;
}
void solve() {
  if (m != n - 1) {
    printf("-1\n");
    return;
  }
  res = 0;
  if (dfs(0, -1)) {
    printf("-1\n");
    return;
  }
  printf("%lld\n", res);
}
int main() {
  precalc();
  while (1) {
    if (!read()) {
      break;
    }
    solve();
  }
  return 0;
}
