#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
const int MAXN = 1e5 + 10;
vector<int> edge[MAXN], cycle;
struct DSU {
  int par[MAXN];
  DSU() {
    for (register int i = (1); i <= int(MAXN - 1); ++i) {
      par[i] = i;
    }
  }
  inline int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }
  inline void marry(int x, int y) { par[find(x)] = find(y); }
} Dsu;
int val[MAXN], par[MAXN];
inline void dfs(int u, int valor) {
  val[u] = valor;
  for (auto v : edge[u]) {
    if (v == par[u]) continue;
    par[v] = u;
    dfs(v, -valor);
    val[u] += val[v];
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  int le1 = 1, le2 = 1;
  int n, m, u, v;
  cin >> n >> m;
  for (register int i = (1); i <= int(m); ++i) {
    cin >> u >> v;
    if (Dsu.find(u) != Dsu.find(v)) {
      Dsu.marry(u, v);
      edge[u].push_back(v);
      edge[v].push_back(u);
    } else {
      le1 = u, le2 = v;
    }
  }
  dfs(le1, 1);
  if (n == m) {
    for (int u = le2; u; u = par[u]) {
      cycle.push_back(val[u]);
    }
    if (cycle.size() & 1) {
      if (val[le1] & 1) {
        cout << -1 << '\n';
        return 0;
      }
      for (int u = le2; u; u = par[u]) {
        val[u] -= val[le1] / 2;
      }
    } else {
      if (val[le1]) {
        cout << -1 << '\n';
        return 0;
      }
      sort((cycle).begin(), (cycle).end());
      for (int u = le2; u; u = par[u]) {
        val[u] -= cycle[cycle.size() / 2 - 1];
      }
    }
  } else if (val[le1]) {
    cout << -1 << '\n';
    return 0;
  }
  long long ans = 0;
  for (register int i = (1); i <= int(n); ++i) {
    ans += abs(val[i]);
  }
  cout << ans << '\n';
  return 0;
}
