#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m;
int deep[N];
int token[N];
int capa[N];
pair<int, int> edge[N];
bool in_cycle[N];
bool mark[N];
int id_e = -1;
vector<int> path;
vector<pair<int, int> > G[N];
void dfs(int v = 0, int par = 0) {
  deep[v] = 1 + deep[par];
  for (pair<int, int> u : G[v])
    if (u.second != id_e && u.first != par) dfs(u.first, v);
}
void calc(int v = 0, int par = 0) {
  for (pair<int, int> u : G[v])
    if (u.second != id_e && u.first != par) {
      calc(u.first, v);
      token[v] += token[u.first];
      capa[v] += capa[u.first];
    }
}
bool find_cycle(int v = 0, int par = 0) {
  mark[v] = true;
  for (pair<int, int> u : G[v])
    if (!mark[u.first]) {
      path.push_back(u.second);
      if (find_cycle(u.first, v)) return true;
      path.pop_back();
    } else {
      if (u.first != par) {
        id_e = u.second;
        return true;
      }
    }
  return false;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> edge[i].first >> edge[i].second;
    edge[i].first--, edge[i].second--;
    G[edge[i].first].push_back({edge[i].second, i});
    G[edge[i].second].push_back({edge[i].first, i});
  }
  if (m == n - 1) {
    dfs();
    int mod[2] = {0};
    for (int i = 0; i < n; i++) mod[deep[i] & 1]++;
    if (mod[0] != mod[1]) return cout << "-1\n", 0;
    for (int i = 0; i < n; i++)
      if (deep[i] & 1)
        token[i]++;
      else
        capa[i]++;
    calc();
    int ans = 0;
    for (int i = 0; i < m; i++) {
      if (deep[edge[i].second] < deep[edge[i].first])
        swap(edge[i].first, edge[i].second);
      ans += abs(token[edge[i].second] - capa[edge[i].second]);
    }
    return cout << ans << "\n", 0;
  }
  find_cycle();
  int ver = edge[id_e].first;
  path.clear();
  memset(mark, 0, sizeof mark);
  find_cycle(ver);
  if (path.size() & 1) {
    for (int id : path) in_cycle[id] = true;
    in_cycle[id_e] = true;
    dfs();
    int mod[2] = {0};
    for (int i = 0; i < n; i++) mod[deep[i] & 1]++;
    if (mod[0] != mod[1]) return cout << "-1\n", 0;
    for (int i = 0; i < n; i++)
      if (deep[i] & 1)
        token[i]++;
      else
        capa[i]++;
    calc();
    vector<int> pp;
    pp.push_back(0);
    int ans = 0;
    for (int i = 0; i < n; i++) {
      if (deep[edge[i].second] < deep[edge[i].first])
        swap(edge[i].first, edge[i].second);
      if (in_cycle[i])
        pp.push_back(token[edge[i].second] - capa[edge[i].second]);
      else
        ans += abs(token[edge[i].second] - capa[edge[i].second]);
    }
    sort(pp.begin(), pp.end());
    for (int i = 0; i < pp.size(); i++) ans += abs(pp[i] - pp[pp.size() / 2]);
    cout << ans << "\n";
    return 0;
  }
  exit(-1);
  return 0;
}
