#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
struct V {
  int head;
} v[N];
struct E {
  int u, v, next;
} e[N * 2];
int n, m, ecnt = 1, degree[N];
inline void add(int x, int y) {
  e[++ecnt].u = x, e[ecnt].v = y;
  e[ecnt].next = v[x].head, v[x].head = ecnt;
  e[++ecnt].u = y, e[ecnt].v = x;
  e[ecnt].next = v[y].head, v[y].head = ecnt;
  degree[x]++, degree[y]++;
}
int size[N];
long long ans;
int fa[N];
bool vis[N];
void dfs(int x, int f, int fl) {
  size[x] += fl, vis[x] = 1;
  for (int i = v[x].head; i; i = e[i].next) {
    int y = e[i].v;
    if (y == f) continue;
    if (!vis[y]) dfs(y, x, fl * -1);
    size[x] += size[y];
  }
  ans += abs(size[x]);
}
inline int find(int x) { return fa[x] = (x == fa[x] ? x : find(fa[x])); }
inline void uni(int x, int y) { fa[find(x)] = find(y); }
void get(int x) {
  for (int i = v[x].head; i; i = e[i].next) {
    int y = e[i].v;
    if (!fa[y]) fa[y] = x, get(y);
  }
}
int main() {
  scanf("%d %d\n", &n, &m);
  int tu, tv;
  int U, V;
  for (int i = (1); i <= (m); ++i) {
    scanf("%d %d\n", &tu, &tv), add(tu, tv);
    if (find(tu) == find(tv))
      U = tu, V = tv;
    else
      uni(tu, tv);
  }
  if (n == m) {
    memset(fa, 0, sizeof(fa));
    get(U);
    int top = 0, s[N];
    for (int i = V; i; i = fa[i]) s[++top] = i;
    if (top & 1) {
      dfs(U, 0, 1);
      int temp = size[U];
      if (temp % 2) {
        printf("-1\n");
        return 0;
      } else {
        for (int i = (1); i <= (n); ++i) size[U] = 0;
        size[U] -= temp / 2, size[V] -= temp / 2;
        memset(vis, 0, sizeof(vis));
        dfs(U, 0, 1);
        printf("%lld\n", ans + temp / 2);
      }
    } else {
    }
  } else {
    for (int i = (1); i <= (n); ++i)
      if (degree[i] == 1) {
        dfs(i, 0, 1);
        if (!size[i])
          printf("%lld\n", ans);
        else
          printf("-1\n");
        return 0;
      }
  }
  return 0;
}
