#include <bits/stdc++.h>
using namespace std;
int n, m;
namespace subtask1 {
struct edge {
  int to, nxt;
} e[100005 << 1];
int et, head[100005], col[100005], res;
inline void adde(int x, int y) { e[++et] = (edge){y, head[x]}, head[x] = et; }
inline void dfs(int x, int fa = 0) {
  for (int i = head[x]; i; i = e[i].nxt)
    if (e[i].to != fa) dfs(e[i].to, x);
  if (!col[x]) col[x] ^= 1, col[fa] ^= 1, res++;
}
inline void main() {
  for (int i = 1, x, y; i <= m; i++)
    scanf("%d%d", &x, &y), adde(x, y), adde(y, x);
  memset(col, 0, sizeof(col)), res = 0, dfs(1),
                               printf("%d\n", col[0] ? -1 : res);
}
}  // namespace subtask1
namespace subtask2 {
struct edge {
  int to, nxt;
} e[100005 << 1];
int et, dis, mn, head[100005], col[100005], res, c[100005], tot, vis[100005],
    cnt, cir[100005];
inline void adde(int x, int y) { e[++et] = (edge){y, head[x]}, head[x] = et; }
inline int fcir(int x, int fa = 0) {
  if (vis[x])
    return x;
  else
    vis[x] = -1;
  for (int i = head[x], t; i; i = e[i].nxt)
    if (e[i].to != fa) {
      t = fcir(e[i].to, x);
      if (t) return vis[x] = 1, cir[++cnt] = e[i].to, t != x ? t : 0;
    }
  return 0;
}
inline void dfs(int x, int fa = 0) {
  for (int i = head[x]; i; i = e[i].nxt)
    if (e[i].to != fa && vis[e[i].to] != 1) dfs(e[i].to, x);
  if (fa == 0)
    return;
  else if (!col[x])
    col[x] ^= 1, col[fa] ^= 1, res++;
}
inline int lst(int x) { return x == 1 ? n : x - 1; }
inline int nxt(int x) { return x == n ? 1 : x + 1; }
inline void main() {
  for (int i = 1, x, y; i <= m; i++)
    scanf("%d%d", &x, &y), adde(x, y), adde(y, x);
  memset(col, 0, sizeof(col)), cnt = 0, res = 0, fcir(1), tot = 0, dis = 0,
                               mn = 1e9;
  for (int i = 1; i <= cnt; i++) dfs(cir[i]);
  for (int i = 1; i <= cnt; i++)
    if (!col[cir[i]]) c[++tot] = i;
  for (int i = 1; i <= tot; i += 2) dis += abs(c[i] - c[lst(i)]);
  mn = min(mn, dis), dis = 0;
  for (int i = 1; i <= tot; i += 2) dis += abs(c[i] - c[nxt(i)]);
  mn = min(mn, dis), res += mn, printf("%d\n", res);
}
}  // namespace subtask2
int main() {
  scanf("%d%d", &n, &m);
  if (n == m)
    subtask2::main();
  else
    subtask1::main();
  return 0;
}
