#include <bits/stdc++.h>
template <class T>
inline void read(T &res) {
  res = 0;
  bool bo = 0;
  char c;
  while (((c = getchar()) < '0' || c > '9') && c != '-')
    ;
  if (c == '-')
    bo = 1;
  else
    res = c - 48;
  while ((c = getchar()) >= '0' && c <= '9')
    res = (res << 3) + (res << 1) + (c - 48);
  if (bo) res = ~res + 1;
}
const int N = 1e5 + 5, M = N << 1;
int n, m, ecnt, nxt[M], adj[N], go[M], dep[N], fa[N], cnt1, sze[2][N], tot,
    a[N], val[M], pos[M], cnt[M][2], s[M], tmp[M];
bool vis[N], isc[N];
long long ans;
void add_edge(int u, int v) {
  nxt[++ecnt] = adj[u];
  adj[u] = ecnt;
  go[ecnt] = v;
  nxt[++ecnt] = adj[v];
  adj[v] = ecnt;
  go[ecnt] = u;
}
void dfs(int u, int fu) {
  dep[u] = dep[fu] + 1;
  if (dep[u] & 1) cnt1++;
  sze[dep[u] & 1][u] = 1;
  for (int e = adj[u], v; e; e = nxt[e])
    if ((v = go[e]) != fu)
      dfs(v, u), sze[0][u] += sze[0][v], sze[1][u] += sze[1][v];
  ans += sze[0][u] > sze[1][u] ? sze[0][u] - sze[1][u] : sze[1][u] - sze[0][u];
}
void fcir(int u, int fu) {
  vis[u] = 1;
  dep[u] = dep[fu] + 1;
  fa[u] = fu;
  for (int e = adj[u], v; e; e = nxt[e])
    if ((v = go[e]) != fu && !vis[v])
      fcir(v, u);
    else if (v != fu && dep[v] < dep[u])
      for (int x = u; x != fa[v]; x = fa[x]) isc[a[++tot] = x] = 1;
}
void sfd(int u, int fu) {
  dep[u] = dep[fu] + 1;
  sze[dep[u] & 1][u] = 1;
  for (int e = adj[u], v; e; e = nxt[e])
    if ((v = go[e]) != fu && !isc[v])
      sfd(v, u), sze[0][u] += sze[0][v], sze[1][u] += sze[1][v];
  if (fu)
    ans +=
        sze[0][u] > sze[1][u] ? sze[0][u] - sze[1][u] : sze[1][u] - sze[0][u];
}
int main() {
  freopen("color.in", "r", stdin);
  freopen("color.out", "w", stdout);
  int x, y;
  read(n);
  read(m);
  for (int i = 1; i <= m; i++) read(x), read(y), add_edge(x, y);
  if (m == n - 1) {
    dfs(1, 0);
    if (cnt1 * 2 != n) return puts("-1"), 0;
    return std::cout << ans << std::endl, 0;
  }
  fcir(1, 0);
  m = 0;
  for (int i = 1; i <= tot; i++) {
    sfd(a[i], 0);
    if (i & 1) std::swap(sze[0][a[i]], sze[1][a[i]]);
    if (sze[0][a[i]] > sze[1][a[i]])
      for (int j = 1; j <= sze[0][a[i]] - sze[1][a[i]]; j++)
        val[++m] = 0, pos[m] = i;
    else
      for (int j = 1; j <= sze[1][a[i]] - sze[0][a[i]]; j++)
        val[++m] = 1, pos[m] = i;
  }
  for (int i = 1; i <= m; i++)
    val[i + m] = tot & 1 ? val[i] ^ 1 : val[i], pos[i + m] = pos[i] + tot;
  for (int i = 1; i <= (m << 1); i++)
    cnt[i][0] = cnt[i - 1][0] + (val[i] == 0),
    cnt[i][1] = cnt[i - 1][1] + (val[i] == 1);
  if (tot & 1) {
    for (int i = 1; i <= m; i++)
      if (cnt[i + m][0] - cnt[i][0] == cnt[i + m][1] - cnt[i][1]) {
        int c0 = 0, c1 = 0;
        for (int j = i + 1; j <= i + m; j++)
          (val[j] ? c1 : c0)++, ans += 1ll * (pos[j + 1] - pos[j]) *
                                       (c0 > c1 ? c0 - c1 : c1 - c0);
        return std::cout << ans << std::endl, 0;
      }
    return puts("-1"), 0;
  }
  int c0 = 0, c1 = 0;
  for (int i = 1; i <= m; i++) (val[i] ? c1 : c0)++;
  if (c0 != c1) return puts("-1"), 0;
  for (int i = 1; i <= m; i++) tmp[pos[i]] += val[i] ? 1 : -1;
  for (int i = 1; i <= tot; i++) tmp[i] += tmp[i - 1];
  std::sort(tmp + 1, tmp + tot + 1);
  int mid = tot + 1 >> 1;
  for (int i = 1; i <= tot; i++)
    ans += i <= mid ? tmp[mid] - tmp[i] : tmp[i] - tmp[mid];
  return std::cout << ans << std::endl, 0;
}
