#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int tt;
int head[N], to[N * 2], nxt[N * 2], sz[N], fa[N];
inline int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
inline void dfs(int x, int f, int d) {
  sz[x] = d, fa[x] = f;
  for (int i = head[x]; i; i = nxt[i]) {
    int y = to[i];
    if (y != f) dfs(y, x, -d), sz[x] += sz[y];
  }
}
inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
  int n, m, A, B;
  long long ans = 0;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int x = gi(), y = gi();
    if (find(x) == find(y))
      A = x, B = y;
    else {
      fa[find(x)] = find(y);
      to[++tt] = y, nxt[tt] = head[x], head[x] = tt;
      to[++tt] = x, nxt[tt] = head[y], head[y] = tt;
    }
  }
  dfs(1, 0, 1);
  if (m == n - 1) {
    if (sz[1]) return puts("-1"), 0;
    for (int i = 1; i <= n; i++) ans += abs(sz[i]);
    cout << ans;
  }
  return 0;
}
