#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int N, M;
vector<int> E[MAXN];
int sum[MAXN];
void load() {
  scanf("%d%d", &N, &M);
  while (M--) {
    int a, b;
    scanf("%d%d", &a, &b);
    E[a].push_back(b);
    E[b].push_back(a);
  }
}
int dfs(int x, int p, int d) {
  sum[x] = d;
  for (int i = 0; i < E[x].size(); i++) {
    int nxt = E[x][i];
    if (nxt == p) continue;
    sum[x] += dfs(nxt, x, -d);
  }
  return sum[x];
}
long long solve() {
  dfs(1, 0, 1);
  if (sum[1]) return -1;
  long long sol = 0;
  for (int i = 1; i <= N; i++) sol += abs(sum[i]);
  return sol;
}
int main() {
  load();
  printf("%lld\n", solve());
  return 0;
}
