#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, m, dep[maxn], cir[maxn];
int tote = 1, FIR[maxn], NEXT[maxn], TO[maxn];
bool vis[maxn];
void addedge(int u, int v) {
  TO[++tote] = v;
  NEXT[tote] = FIR[u];
  FIR[u] = tote;
}
namespace Tree {
int Ans, c1, c2;
int dfs(int u, int f) {
  int sz = dep[u];
  dep[u] == 1 ? c1++ : c2++;
  for (int p = FIR[u]; p; p = NEXT[p]) {
    int v = TO[p];
    if (v == f) continue;
    dep[v] = -dep[u];
    sz += dfs(v, u);
  }
  Ans += abs(sz);
  return sz;
}
void solve() {
  dep[1] = 1;
  dfs(1, 0);
  if (c1 != c2)
    puts("-1");
  else
    printf("%d\n", Ans);
}
};  // namespace Tree
namespace OddCircleTree {
int Ans, c1, c2, del;
void dfs1(int u, int f) {
  dep[u] == 1 ? c1++ : c2++;
  for (int p = FIR[u]; p; p = NEXT[p]) {
    int v = TO[p];
    if (v == f) continue;
    if (p == del || (p ^ 1) == del) continue;
    dep[v] = -dep[u];
    dfs1(v, u);
  }
}
int dfs2(int u, int f) {
  int sz = dep[u];
  for (int p = FIR[u]; p; p = NEXT[p]) {
    int v = TO[p];
    if (v == f) continue;
    if (p == del || (p ^ 1) == del) continue;
    sz += dfs2(v, u);
  }
  Ans += abs(sz);
  return sz;
}
void solve() {
  int u = cir[1];
  for (int p = FIR[u]; p; p = NEXT[p]) {
    int v = TO[p];
    if (v == cir[2]) del = p;
  }
  dep[cir[1]] = 1;
  dfs1(cir[1], 0);
  if ((c1 ^ c2) & 1)
    puts("-1");
  else {
    Ans = abs((c1 - c2) / 2);
    dep[cir[1]] -= (c1 - c2) / 2;
    dep[cir[2]] -= (c1 - c2) / 2;
    dfs2(cir[1], 0);
    printf("%d\n", Ans);
  }
}
}  // namespace OddCircleTree
namespace EvenCircleTree {
int Ans, c1, c2, del;
int k[maxn], b[maxn], b0[maxn];
void dfs(int u, int f) {
  dep[u] == 1 ? c1++ : c2++;
  b[u] = dep[u];
  k[u] += u == cir[2];
  for (int p = FIR[u]; p; p = NEXT[p]) {
    int v = TO[p];
    if (v == f) continue;
    if (p == del || (p ^ 1) == del) continue;
    dep[v] = -dep[u];
    dfs(v, u);
    b[u] += b[v];
    k[u] += k[v];
  }
}
void solve() {
  int u = cir[1];
  for (int p = FIR[u]; p; p = NEXT[p]) {
    int v = TO[p];
    if (v == cir[2]) del = p;
  }
  dep[cir[1]] = 1;
  dfs(cir[1], 0);
  if (c1 != c2)
    puts("-1");
  else {
    Ans = 0x7fffffff;
    int i, K = -cir[0], B = 0;
    for (i = 1; i <= n; i++)
      if (!k[i])
        B += b[i];
      else {
        B -= b[i];
        b0[++b0[0]] = -b[i];
      }
    sort(b0 + 1, b0 + b0[0] + 1);
    int p = 1;
    for (i = -n; i <= n; i++) {
      while (b0[p] == i && p <= b0[0]) K += 2, B -= i + i, p++;
      if (Ans > (long long)K * i + B) Ans = K * i + B;
    }
    printf("%d\n", Ans);
  }
}
}  // namespace EvenCircleTree
int findcircle(int u, int f) {
  vis[u] = 1;
  for (int p = FIR[u]; p; p = NEXT[p]) {
    int v = TO[p];
    if (v == f) continue;
    if (vis[v]) {
      cir[++cir[0]] = u;
      return v;
    }
    int tmp = findcircle(v, u);
    if (tmp) {
      cir[++cir[0]] = u;
      if (u == tmp)
        return 0;
      else
        return tmp;
    }
  }
  return 0;
}
int main() {
  int i, u, v;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &u, &v);
    addedge(u, v);
    addedge(v, u);
  }
  if (n != m)
    Tree::solve();
  else {
    findcircle(1, 0);
    if (cir[0] & 1)
      OddCircleTree::solve();
    else
      EvenCircleTree::solve();
  }
}
