#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100005];
int depth[100005];
int value[100005];
int sz[100005];
int backLow, backHigh;
void dfs(int u, int p) {
  for (int v : adj[u]) {
    if (v == p) continue;
    if (depth[v] != 0) {
      backLow = u;
      backHigh = v;
      continue;
    }
    depth[v] = depth[u] + 1;
    dfs(v, u);
  }
}
void dp(int u) {
  sz[u] = value[u];
  for (int v : adj[u]) {
    if (depth[v] == depth[u] + 1) {
      dp(v);
      sz[u] += sz[v];
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  depth[0] = 1;
  dfs(0, -1);
  if (m == n - 1) {
    for (int i = 0; i < n; i++) {
      if (depth[i] & 1)
        value[i] = 1;
      else
        value[i] = -1;
    }
    dp(0);
    long long ans = 0;
    long long balance = 0;
    for (int i = 0; i < n; i++) {
      ans += abs(sz[i]);
      balance += value[i];
    }
    if (balance != 0) ans = -1;
    cout << ans;
  } else if ((depth[backLow] - depth[backHigh]) % 2 == 0) {
    for (int i = 0; i < n; i++) {
      if (depth[i] & 1)
        value[i] = 1;
      else
        value[i] = -1;
    }
    long long balance = 0;
    for (int i = 0; i < n; i++) {
      balance += value[i];
    }
    if (balance & 1) {
      cout << -1;
      return 0;
    }
    value[backHigh] -= balance / 2;
    value[backLow] -= balance / 2;
    dp(0);
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      ans += abs(sz[i]);
    }
    ans += abs(balance) / 2;
    cout << ans;
  } else {
    assert(false);
  }
}
