#include <bits/stdc++.h>
using namespace std;
int n, m;
long long ans;
int tt[200005], he[100005], nx[200005], _;
void add(int x, int y) { nx[++_] = he[x], he[x] = _, tt[_] = y; }
int dfs(int x, int y, int z) {
  int s = z;
  for (int i = he[x]; i; i = nx[i])
    if (tt[i] != y) s += dfs(tt[i], x, -z);
  ans += abs(s);
  return s;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y), add(y, x);
  }
  if (dfs(1, 0, 1))
    puts("-1");
  else
    printf("%d\n", ans);
  return 0;
}
