#include <bits/stdc++.h>
using namespace std;
int N, M;
vector<unordered_set<int> > G;
vector<vector<int> > T;
long long res;
void build(int a, int b) {
  unordered_set<int>::iterator it;
  for (it = G[a].begin(); it != G[a].end(); ++it) {
    int c = *it;
    if (c == b) continue;
    T[a].push_back(c);
    build(c, a);
  }
  return;
}
int cal(int a) {
  int now = 1;
  for (int i = 0; i < T[a].size(); i++) {
    now -= cal(T[a][i]);
  }
  if (a != 1) {
    res += abs(now);
  }
  return now;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> N >> M;
  G.resize(N + 1);
  int a, b;
  for (int i = 0; i < M; i++) {
    cin >> a >> b;
    G[a].insert(b);
    G[b].insert(a);
  }
  if (N == M) {
    res = 0;
    queue<int> X;
    for (int i = 1; i <= N; i++) {
      if (G[i].size() == 1) X.push(i);
    }
    int need[100010];
    for (int i = 1; i <= N; i++) need[i] = 1;
    while (X.size() > 0) {
      a = X.front();
      X.pop();
      unordered_set<int>::iterator it;
      it = G[a].begin();
      b = *it;
      res += abs(need[a]);
      need[b] -= need[a];
      G[b].erase(a);
      if (G[b].size() == 1) X.push(b);
    }
    int start = -1;
    for (int i = 1; i <= N; i++) {
      if (G[i].size() == 2) {
        start = i;
        break;
      }
    }
    vector<long long> route;
    route.push_back(start);
    unordered_set<int>::iterator it;
    it = G[start].begin();
    int pre = start;
    int crt = *it;
    while (crt != start) {
      route.push_back(crt);
      it = G[crt].begin();
      if (*it == pre) it++;
      pre = crt;
      crt = *it;
    }
    vector<long long> cost, pos, neg, all;
    long long zero = 0;
    for (int i = 0; i < route.size() - 1; i++) {
      res += abs(need[route[i]]);
      cost.push_back(need[route[i]]);
      need[route[i + 1]] -= need[route[i]];
    }
    if (need[route[route.size() - 1]] % 2 != 0) {
      cout << -1 << endl;
      return 0;
    }
    long long rem = need[route[route.size() - 1]] / 2;
    for (int i = 0; i < cost.size(); i++) {
      res -= abs(cost[i]) - abs(cost[i] - rem);
      cost[i] -= rem;
      rem *= -1;
    }
    res += abs(rem);
    cost.push_back(-rem);
    if (route.size() % 2) {
      cout << res << endl;
      return 0;
    }
    for (int i = 0; i < cost.size(); i++) {
      all.push_back(abs(cost[i]));
    }
    sort(all.begin(), all.end());
    long long num = all[all.size() / 2];
    for (int i = 0; i < all.size(); i++) {
      res -= abs(all[i]) - abs(all[i] - num);
    }
    cout << res << endl;
    return 0;
  } else {
    T.resize(N + 1);
    build(1, 0);
    res = 0;
    int ok = cal(1);
    if (ok != 0)
      cout << -1 << endl;
    else
      cout << res << endl;
  }
  return 0;
}
