#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int son[N], gson[N];
struct E {
  int t, xt;
} e[N << 1];
int h[N], cnt;
void adge(int f, int t) {
  e[++cnt] = (E){t, h[f]};
  h[f] = cnt;
}
int ans = 0;
void dfs(int u, int fa) {
  int s = 0;
  for (int i = h[u]; ~i; i = e[i].xt)
    if (e[i].t != fa) {
      dfs(e[i].t, u);
      son[u]++;
      gson[u] += son[e[i].t];
      if (son[e[i].t]) s++;
    }
  son[u] -= gson[u];
  if (son[u] < 0) {
    puts("-1");
    exit(0);
  }
  ans += gson[u] * 3 - (s << 1);
}
int main() {
  memset(h, cnt = -1, sizeof(h));
  int n, m;
  scanf("%d%d", &n, &m);
  if (n == m) return 0;
  for (int i = 1; i <= m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    adge(a, b);
    adge(b, a);
  }
  adge(0, 1);
  dfs(0, 0);
  if (son[0] == 0)
    printf("%d\n", ans);
  else
    puts("-1");
  return 0;
}
