#include <bits/stdc++.h>
using namespace std;
const long long int N = 2e5 + 10;
long long int n, m;
vector<long long int> adj[N];
vector<long long int> cycle;
bool vis[N], cyc[N];
long long int find_cycle(long long int u, long long int p) {
  if (vis[u]) return u;
  vis[u] = true;
  long long int res = -1;
  for (long long int v : adj[u])
    if (v != p) {
      res = max(res, find_cycle(v, u));
    }
  if (res != -1) {
    cyc[u] = true;
    cycle.push_back(u);
    if (u == res) res = -1;
  }
  return res;
}
long long ll res = 0;
long long int bal[N];
void dfs(long long int u, long long int p) {
  bal[u] = 1;
  for (long long int v : adj[u])
    if (!cyc[v] && v != p) {
      dfs(v, u);
      bal[u] -= bal[v];
    }
  if (p != -1) res += abs(bal[u]);
}
void result(long long ll res) {
  cout << res << '\n';
  exit(0);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  memset(vis, 0, sizeof vis);
  memset(cyc, 0, sizeof cyc);
  cin >> n >> m;
  for (long long int _ = 0; _ < (long long int)(m); _++) {
    long long int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  if (m == n) {
    find_cycle(0, -1);
    vector<long long int> cbal;
    for (long long int r : cycle) {
      dfs(r, -1);
      cbal.push_back(bal[r]);
    }
    if (((long long int)(cycle).size()) % 2) {
      long long ll total = 0;
      for (long long int i = 0;
           i < (long long int)(((long long int)(cbal).size())); i++) {
        if (i % 2 == 0)
          total -= cbal[i];
        else
          total += cbal[i];
      }
      if (total % 2) result(-1);
      total /= 2;
      for (auto b : cbal) {
        res += abs(total);
        total = -total - b;
      }
      result(res);
    } else {
      vector<long long ll> cum;
      cum.push_back(0);
      for (long long int i = 0;
           i < (long long int)(((long long int)(cbal).size())); i++) {
        if (i % 2 == 0)
          cum.push_back(cum.back() - cbal[i]);
        else
          cum.push_back(cum.back() + cbal[i]);
      }
      if (cum.back() != 0)
        result(-1);
      else {
        cum.pop_back();
        sort((cum).begin(), (cum).end());
        long long ll med = cum[((long long int)(cum).size()) / 2];
        for (long long ll x : cum) res += abs(x - med);
        result(res);
      }
    }
  } else {
    dfs(0, -1);
    if (bal[0] != 0) result(-1);
    result(res);
  }
  return 0;
}
