#include <bits/stdc++.h>

using namespace std;

const int N = 1234567;

vector <int> g[N];
int was[N];

vector <int> c;
bool found;

int nnn;

void dfs_cycle(int v, int pr) {
  if (found) {
    return;
  }
  c.push_back(v);
  was[v] = 1;
  int sz = g[v].size();
  for (int j = 0; j < sz; j++) {
    int u = g[v][j];
    if (u == pr) {
      continue;
    }
    if (was[u] == 0) {
      dfs_cycle(u, v);
      if (found) {
        return;
      }
      continue;
    }
    if (was[u] == 1) {
      vector <int> new_c;
      for (int pos = 0; pos < (int) c.size(); pos++) {
        if (c[pos] == u) {
          for (int z = pos; z < (int) c.size(); z++) {
            new_c.push_back(c[z]);
          }
          break;
        }
      }
      c = new_c;
      found = true;
      return;
    }
  }
  c.pop_back();
}

vector <int> find_cycle() {
  c.clear();
  found = false;
  dfs_cycle(0, -1);
  return c;
}

long long ans;
bool on_cycle[N];

long long dfs(int v, int pr) {
  int sz = g[v].size();
  long long sum = 0;
  for (int j = 0; j < sz; j++) {
    int u = g[v][j];
    if (u == pr || on_cycle[u]) {
      continue;
    }
    long long cur = dfs(u, v);
    sum += cur;
  }
  long long up = 1 - sum;
  ans += (up > 0 ? up : -up);
  return up;
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    x--; y--;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  if (m == n - 1) {
    ans = 0;
    if (dfs(0, -1) != 0) {
      cout << -1 << endl;
    } else {
      cout << ans << endl;
    }
  } else {
    vector <int> cycle = find_cycle();
    assert(!cycle.empty());
    for (int i = 0; i < n; i++) {
      on_cycle[i] = false;
    }
    for (int p : cycle) {
      on_cycle[p] = true;
    }
    vector <long long> z;
    ans = 0;
    for (int i = 0; i < (int) cycle.size(); i++) {
      z.push_back(dfs(cycle[i], -1));
      ans -= (z.back() > 0 ? z.back() : -z.back());
    }
    int m = z.size();
    if (m % 2 == 1) {
      long long total = 0;
      for (int i = m - 1; i >= 0; i--) {
        if (i % 2 == (m - 1) % 2) {
          total += z[i];
        } else {
          total -= z[i];
        }
      }
      if (total % 2 != 0) {
        cout << -1 << endl;
      } else {
        long long x = total / 2;
        for (int i = 0; i < m; i++) {
          ans += (x > 0 ? x : -x);
          x = z[i] - x;
        }
        cout << ans << endl;
      }
    } else {
      long long total = 0;
      for (int i = m - 1; i >= 0; i--) {
        if (i % 2 == (m - 1) % 2) {
          total += z[i];
        } else {
          total -= z[i];
        }
      }
      if (total != 0) {
        cout << -1 << endl;
      } else {
        vector <long long> h;
        long long value = 0;
        for (int i = 0; i < m; i++) {
          if (i % 2 == 0) {
            h.push_back(-value);
          } else {
            h.push_back(value);
          }
          value = z[i] - value;
        }
        sort(h.begin(), h.end());
        long long x = h[m / 2];
        for (int i = 0; i < m; i++) {
          ans += (x > 0 ? x : -x);
          x = z[i] - x;
        }
        cout << ans << endl;
      }
    }
  }
  return 0;
}


Measured memory_bytes: 45296