#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 5;
int n, m, ans;
int edge[MAX_N];
bool inCycle[MAX_N];
int len;
bool mark[MAX_N];
vector<int> G[MAX_N], cycle;
int dp[MAX_N];
int Arr[MAX_N];
set<int> in, out;
set<pair<int, pair<int, int> > > dis;
void calcDp(int v, int p = 0, int h = 0) {
  dp[v] = (h & 1 ? 1 : -1);
  mark[v] = true;
  for (int id : G[v]) {
    if (inCycle[id]) continue;
    int u = edge[id] ^ v;
    if (u != p) {
      calcDp(u, v, h + 1);
      dp[v] += dp[u];
    }
  }
  if (v != p) ans += abs(dp[v]);
}
void solve1() {
  calcDp(0);
  if (dp[0])
    cout << "-1\n";
  else
    cout << ans << "\n";
}
void add(int v) {
  auto it2 = out.lower_bound(v);
  if (it2 == out.begin()) it2 = out.end();
  it2--;
  int u = *it2;
  int tmp = min(abs(u - v), min(v, u) + (int)cycle.size() - max(v, u));
  dis.insert({tmp, {v, u}});
  it2 = out.lower_bound(v);
  if (it2 == out.end()) it2 = out.begin();
  u = *it2;
  tmp = min(abs(u - v), min(v, u) + (int)cycle.size() - max(v, u));
  dis.insert({tmp, {v, u}});
}
void solve3() {
  int sum = 0;
  for (int i = 0; i < cycle.size(); i++) {
    int v = cycle[i];
    calcDp(v, v, i & 1);
    Arr[i] = abs(dp[v]);
    if (0 < dp[v]) in.insert(i);
    if (dp[v] < 0) out.insert(i);
    sum += dp[v];
  }
  if (sum != 0) {
    cout << "-1\n";
    return;
  }
  for (auto it : in) add(it);
  while (in.size()) {
    auto it = dis.begin();
    int w = it->first;
    int v = it->second.first;
    int u = it->second.second;
    dis.erase(dis.begin());
    int d = min(Arr[v], Arr[u]);
    ans += w * d;
    Arr[v] -= d;
    Arr[u] -= d;
    if (!Arr[u]) out.erase(u);
    if (!Arr[v])
      in.erase(v);
    else
      add(v);
  }
  cout << ans << "\n";
}
int FindCycle(int v, int p = 0) {
  if (mark[v]) return v;
  mark[v] = true;
  for (int id : G[v]) {
    int u = edge[id] ^ v;
    if (u != p) {
      int res = FindCycle(u, v);
      if (res != -1) {
        if (res == n) return n;
        inCycle[id] = true;
        len++;
        cycle.push_back(v);
        return (v == res ? n : res);
      }
    }
  }
  return -1;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int v, u;
    cin >> v >> u;
    v--, u--;
    edge[i] = v ^ u;
    G[v].push_back(i);
    G[u].push_back(i);
  }
  if (n & 1) {
    cout << "-1\n";
    return 0;
  }
  if (m == n - 1) {
    solve1();
    return 0;
  }
  FindCycle(0);
  memset(mark, 0, sizeof mark);
  if (len & 1) {
    exit(-1);
  }
  solve3();
  return 0;
}
