#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
template <typename T>
inline void read(T &AKNOI) {
  T x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') flag = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  AKNOI = flag * x;
}
int n, m;
vector<int> g[MAXN];
namespace Tree {
int dep[MAXN], siz[MAXN];
void DFS(int u, int fa) {
  siz[u] = ((dep[u] & 1) ? 1 : -1);
  for (auto v : g[u]) {
    if (v == fa) continue;
    dep[v] = dep[u] + 1;
    DFS(v, u);
    siz[u] += siz[v];
  }
}
void work() {
  DFS(1, 0);
  if (siz[1]) {
    printf("-1\n");
  } else {
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
      ans += abs(siz[i]);
    }
    printf("%d\n", ans);
  }
}
}  // namespace Tree
void init() {
  read(n);
  read(m);
  for (int i = 1, u, v; i <= m; ++i) {
    read(u);
    read(v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
}
void solve() {
  if (n - 1 == m) {
    Tree::work();
  } else {
    printf("-1\n");
  }
}
int main() {
  init();
  solve();
  return 0;
}
