#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream& operator<<(ostream& o, const pair<T, U>& v) {
  return o << "(" << v.first << ", " << v.second << ")";
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& v) {
  o << "{";
  for (int i = 0; i < static_cast<int>(static_cast<int>((v).size())); ++i)
    o << (i ? ", " : "") << v[i];
  return o << "}";
}
template <class T, class U>
ostream& operator<<(ostream& o, const map<T, U>& v) {
  o << "{";
  for (const auto& e : v) o << e << ", ";
  return o << "}";
}
template <class T, class U>
ostream& operator<<(ostream& o, const unordered_map<T, U>& v) {
  o << "{";
  for (const auto& e : v) o << e << ", ";
  return o << "}";
}
template <class T>
ostream& operator<<(ostream& o, const set<T>& v) {
  o << "{";
  for (const auto& e : v) o << e << ", ";
  return o << "}";
}
template <class T>
string to_s(const T& v) {
  ostringstream is;
  is << v;
  return is.str();
}
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
const int dy[] = {1, 0, -1, 0, -1, 1, 1, -1, 0};
set<int> app[100000 + 100];
int siro[100000 + 100];
int kuro[100000 + 100];
int main(int argc, char* argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  if (m != n - 1) return 0;
  for (int i = 0; i < static_cast<int>(m); ++i) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    app[a].insert(b);
    app[b].insert(a);
  }
  int ans = 0;
  queue<int> q;
  for (int i = 0; i < static_cast<int>(n); ++i)
    if (static_cast<int>((app[i]).size()) == 1) {
      q.push(i);
    }
  int vis[100000 + 100] = {};
  while (!q.empty()) {
    queue<int> nq;
    while (!q.empty()) {
      int cv = q.front();
      q.pop();
      if (vis[cv]) continue;
      vis[cv] = 1;
      int ne = *app[cv].begin();
      int d = kuro[cv] - siro[cv];
      if (d == 1) {
        app[cv].erase(ne);
        app[ne].erase(cv);
        if (static_cast<int>((app[ne]).size()) == 1) nq.push(ne);
        continue;
      }
      if (d > 1) {
        siro[cv] += abs(d - 1);
        siro[ne] += abs(d - 1);
      } else {
        kuro[cv] += abs(d - 1);
        kuro[ne] += abs(d - 1);
      }
      app[cv].erase(ne);
      app[ne].erase(cv);
      if (static_cast<int>((app[ne]).size()) == 1) nq.push(ne);
    }
    q = nq;
  }
  for (int i = 0; i < static_cast<int>(n); ++i)
    if (kuro[i] - siro[i] != 1) {
      cout << -1 << '\n';
      return 0;
    }
  for (int i = 0; i < static_cast<int>(n); ++i) ans += kuro[i] + siro[i];
  ans /= 2;
  cout << ans << '\n';
}
