#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T qmin(const T &x, const T &y) {
  return x < y ? x : y;
}
template <typename T>
inline T qmax(const T &x, const T &y) {
  return x > y ? x : y;
}
template <typename T>
inline void getmin(T &x, const T &y) {
  if (y < x) x = y;
}
template <typename T>
inline void getmax(T &x, const T &y) {
  if (y > x) x = y;
}
inline void fileio(const string &s) {
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}
const int inf = (int)1e9 + 7;
const long long linf = (long long)1e18 + 7;
const int N = 1e5 + 7, M = N << 1;
int v[M], nxt[M];
int head[N], dep[N];
int n, m;
inline void addedge(int p, int x, int y) {
  v[p] = y, nxt[p] = head[x], head[x] = p;
}
namespace task1 {
long long ans = 0;
inline int dfs(int u, int p) {
  dep[u] = dep[p] + 1;
  int rest = (dep[u] & 1 ? 1 : -1);
  for (int e = head[u]; e; e = nxt[e]) {
    if (v[e] == p) continue;
    rest += dfs(v[e], u);
  }
  ans += abs(rest);
  return rest;
}
inline int solve() {
  if (dfs(1, 0)) return puts("-1"), 0;
  printf("%lld\n", ans);
  return 0;
}
}  // namespace task1
int main() {
  scanf("%d%d", &n, &m);
  for (register int i = (1); i <= (m); ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    addedge(((i) << 1), x, y);
    addedge(((i) << 1 ^ 1), y, x);
  }
  if (m == n - 1) task1::solve();
  return 0;
}
