#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
using namespace std;
const int N = 200000 + 10;
const long long MOD = 1000000000 + 7;
const long long INF = 1000000010;
const long long LOG = 25;
int n, m, E, O, h[N], par[N], vaz[N], ans, Ev[N], Od[N], mark[N], a[N];
vector<int> G[N];
bool f = 0;
vector<int> pth;
void DFS(int v = 1, int p = 0) {
  mark[v] = 1;
  if (h[v] % 2 == 0)
    E++;
  else
    O++;
  Ev[v]++;
  for (auto u : G[v]) {
    if (mark[u]) continue;
    h[u] = h[v] + 1;
    par[u] = v;
    DFS(u, v);
    Ev[v] += Od[u];
    Od[v] += Ev[u];
  }
  if (p != 0) ans += abs(Ev[v] - Od[v]);
}
void DFS2(int v = 1, int p = 0) {
  mark[v] = 1;
  par[v] = p;
  for (auto u : G[v]) {
    if (u == p) continue;
    if (mark[u] == 0) {
      h[u] = h[v] + 1;
      DFS2(u, v);
    } else if (h[u] < h[v]) {
      if (h[u] % 2 == h[v] % 2) f = 1;
      int nd = v;
      while (nd != par[u]) {
        pth.push_back(nd);
        nd = par[nd];
      }
    }
  }
}
int Solve(int x) {
  int Last = x;
  int res = abs(x);
  for (int i = 0; i < n - 1; i++) {
    Last = Last - a[i];
    res += abs(Last);
  }
  return res;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int v, u;
    cin >> v >> u;
    G[v].push_back(u);
    G[u].push_back(v);
  }
  if (m == n) {
    DFS2(1);
    memset(mark, 0, sizeof mark);
    if (!f) {
      n = pth.size();
      for (auto u : pth) mark[u] = 1;
      for (int i = 0; i < pth.size(); i++) {
        DFS(pth[i]);
        if (i % 2 == 0)
          E += Ev[pth[i]], a[i] = Ev[pth[i]] - Od[pth[i]];
        else
          a[i] = Od[pth[i]] - Ev[pth[i]], E += Od[pth[i]];
      }
      if (n % 2 == 1 || E != n / 2) return cout << -1, 0;
      int l = -n - 1, r = n + 1;
      while (r - l > 1) {
        int md = (l + r) >> 1;
        if (Solve(md + 1) > Solve(md))
          r = md;
        else
          l = md;
      }
      assert(0);
      cout << ans + Solve(r) << '\n';
      return 0;
    }
    return 0;
  }
  DFS(1);
  if (O != E) return cout << -1, 0;
  cout << ans;
  return 0;
}
