#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
struct kkk {
  int y, nt;
} e[N << 1];
int st[N], cf[N];
int sum[N];
int n, m, len;
void ins(int x, int y) {
  e[++len].y = y;
  e[len].nt = st[x];
  st[x] = len;
}
void dfs(int fa, int x) {
  sum[x] += cf[x];
  for (int i = st[x]; i; i = e[i].nt)
    if (e[i].y == fa)
      continue;
    else if (!cf[e[i].y]) {
      cf[e[i].y] = -cf[x];
      dfs(x, e[i].y);
      sum[x] += sum[e[i].y];
    }
}
int main() {
  int u, v;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d", &u, &v);
    ins(u, v);
    ins(v, u);
  }
  dfs(cf[1] = 1, sum[1] = 1);
  for (int i = 1; i <= n; ++i) cf[0] += cf[i];
  if (cf[0]) return puts("-1");
  for (int i = 1; i <= n; ++i) sum[0] += abs(sum[i]);
  printf("%d", sum[0] - 1);
  return 0;
}
