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