#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
int n;
vector<int> g[N];
int h[N];
int dp[N];
long long ans = 0;
void dfs(int v) {
  dp[v] = 1;
  if (h[v] & 1) dp[v] = -1;
  for (int u : g[v]) {
    if (h[u] != -1) continue;
    h[u] = h[v] + 1;
    dfs(u);
    dp[v] += dp[u];
  }
  ans += abs(dp[v]);
  return;
}
int main() {
  int m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int v, u;
    scanf("%d%d", &v, &u);
    v--;
    u--;
    g[v].push_back(u);
    g[u].push_back(v);
  }
  for (int i = 0; i < n; i++) h[i] = -1;
  h[0] = 0;
  dfs(0);
  if (dp[0] != 0) {
    printf("-1\n");
    return 0;
  }
  printf("%lld\n", ans);
  return 0;
}
