#include <bits/stdc++.h>
using std::bitset;
using std::cerr;
using std::cin;
using std::cout;
using std::deque;
using std::endl;
using std::fill;
using std::fixed;
using std::ios_base;
using std::iterator;
using std::map;
using std::max_element;
using std::min_element;
using std::multimap;
using std::multiset;
using std::pair;
using std::queue;
using std::reverse;
using std::set;
using std::setprecision;
using std::sort;
using std::stable_sort;
using std::stack;
using std::string;
using std::swap;
using std::unique;
using std::vector;
long long min(long long a, long long b) { return a < b ? a : b; }
long long max(long long a, long long b) { return a > b ? a : b; }
namespace MySpace {};
long long n, m;
vector<long long> graph[500000];
long long C = -1, D = -1;
long long c[500000];
long long ss[500000];
long long s[500000];
void color(long long v, long long p) {
  if (c[v] != -1) {
    C = v, D = p;
    return;
  }
  if (p == -1)
    c[v] = 0;
  else
    c[v] = 1 ^ c[p];
  for (auto u : graph[v])
    if (u != p) color(u, v);
}
void dfs(long long v, long long p) {
  ss[v] = c[v];
  s[v] = 1;
  for (auto u : graph[v])
    if (u != p)
      if (v == C && u == D)
        continue;
      else if (v == D && u == C)
        continue;
      else
        dfs(u, v), ss[v] += ss[u], s[v] += s[u];
}
long long get(long long cnt) {
  long long res = abs(cnt);
  vector<long long> s1(n), s2(n);
  dfs(0, -1);
  for (long long i = 0; i < n; i++) s2[i] = s[i] - ss[i];
  fill(s, s + n, 0);
  fill(ss, ss + n, 0);
  c[C] -= cnt;
  c[D] += cnt;
  dfs(0, -1);
  for (long long i = 0; i < n; i++) s1[i] = ss[i];
  fill(s, s + n, 0);
  fill(ss, ss + n, 0);
  c[C] += cnt;
  c[D] -= cnt;
  for (long long i = 0; i < n; i++) res += abs(s1[i] - s2[i]);
  return res;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (long long i = 0; i < m; i++) {
    long long v, u;
    cin >> v >> u, v--, u--;
    graph[v].push_back(u);
    graph[u].push_back(v);
  }
  fill(c, c + n, -1);
  if (m + 1 == n) {
    color(0, -1);
    long long cnt = 0;
    for (long long i = 0; i < n; i++) cnt += c[i];
    if (cnt * 2 != n) cout << -1, exit(0);
    dfs(0, -1);
    long long ans = 0;
    for (long long i = 0; i < n; i++) ans += abs(s[i] - 2 * ss[i]);
    cout << ans;
    return 0;
  }
  color(0, -1);
  long long A = -1, B = -1;
  for (long long i = 0; i < n; i++) {
    for (auto it : graph[i]) {
      if (c[i] == c[it]) {
        A = i, B = it;
      }
    }
  }
  if (A == -1) {
    long long cnt = 0;
    for (long long i = 0; i < n; i++) cnt += c[i];
    if (cnt * 2 != n) cout << -1, exit(0);
    long long l = -100000, r = 100000;
    long long ans = 1e9;
    while (l + 2 < r) {
      long long m1 = l * 2 / 3 + r / 3;
      long long m2 = l / 3 + r * 2 / 3;
      long long g1 = get(m1);
      long long g2 = get(m2);
      ans = min(ans, min(g1, g2));
      if (g1 < g2)
        r = m2;
      else
        l = m1;
    }
    for (long long i = l; i <= r; i++) ans = min(ans, get(i));
    cout << ans;
  } else {
    long long cnt = 0;
    for (long long i = 0; i < n; i++) cnt += c[i];
    if (cnt % 2 != (n - cnt) % 2) cout << -1, exit(0);
    if (cnt > n / 2) {
      for (long long i = 0; i < n; i++) c[i] ^= 1;
      cnt = n - cnt;
    }
    long long ans = n / 2 - cnt;
    C = A, D = B;
    vector<long long> s1(n), s2(n);
    dfs(0, -1);
    for (long long i = 0; i < n; i++) s2[i] = s[i] - ss[i];
    fill(s, s + n, 0);
    fill(ss, ss + n, 0);
    c[A] += ans;
    c[B] += ans;
    dfs(0, -1);
    for (long long i = 0; i < n; i++) s1[i] = ss[i];
    for (long long i = 0; i < n; i++) ans += abs(s1[i] - s2[i]);
    cout << ans;
  }
}
