#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

typedef long double ld;

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

#define sz(x) ((int) (x).size())
#define TASK "text"

const int inf = (int) 1.01e9;
const long long infll = (long long) 1.01e18;
const ld eps = 1e-9;
const ld pi = acos((ld) -1);

#ifdef DEBUG
mt19937 mrand(300); 
#else
mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count()); 
#endif

int rnd(int x) {
  return mrand() % x;
}

void precalc() {
}

const int maxn = (int) 2e5 + 5;
int n, m;
vector<int> g[maxn];

bool read() {
  if (scanf("%d%d", &n, &m) < 2) {
    return false;
  }
  for (int i = 0; i < 2 * n; i++) {
    g[i].clear();
  }
  for (int i = 0; i < m; i++) {
    int v, u;
    scanf("%d%d", &v, &u);
    v--;
    u--;
    g[v].push_back(u);
    g[u].push_back(v);
  }
  return true;
}

int used[maxn];

bool getCycle(int v, int p, vector<int> &c) {
  used[v] = true;
  c.push_back(v);
  for (int i = 0; i < sz(g[v]); i++) {
    int u = g[v][i];
    if (u == p) {
      continue;
    }
    if (used[u]) {
      int pos = 0;
      while (c[pos] != u) {
        pos++;
      }
      c.erase(c.begin(), c.begin() + pos);
      return true;
    }
    if (getCycle(u, v, c)) {
      return true;
    }
  }
  c.pop_back();
  return false;
}

pair<int, long long> solveTree(int v, int p, int t) {
  pair<int, long long> res((t ? 1 : -1), 0);
  for (int i = 0; i < sz(g[v]); i++) {
    int u = g[v][i];
    if (u == p) {
      continue;
    }
    pair<int, long long> cur = solveTree(u, v, !t);
    res.first += cur.first;
    res.second += cur.second + abs(cur.first);
  }
  return res;
}

int x[maxn];

long long solveEven(const vector<int> &c) {
  int k = sz(c);
  for (int i = 0; i < k; i++) {
    int v = c[i], u = c[(i + 1) % k];
    g[v].erase(find(g[v].begin(), g[v].end(), u));
    g[u].erase(find(g[u].begin(), g[u].end(), v));
  }
  long long res = 0;
  x[0] = 0;
  for (int i = 0; i < k; i++) {
    pair<int, long long> cur = solveTree(c[i], -1, (i & 1));
    res += cur.second;
    x[i + 1] = x[i] + cur.first;
  }
  if (x[k]) {
    return -1;
  }
  sort(x, x + k);
  int val = x[k / 2];
  for (int i = 0; i < k; i++) {
    res += abs(x[i] - val);
  }
  return res;
}

void solve() {
  if (n & 1) {
    printf("-1\n");
    return;
  }
  if (m == n - 1) {
    pair<int, long long> res = solveTree(0, -1, 0);
    if (res.first) {
      printf("-1\n");
    } else {
      printf("%lld\n", res.second);
    }
    return;
  }
  for (int i = 0; i < n; i++) {
    used[i] = false;
  }
  vector<int> c;
  getCycle(0, -1, c);
  if (!(sz(c) & 1)) {
    printf("%lld\n", solveEven(c));
    return;
  }
  {
    int v = c[0], u = c.back();
    g[v].erase(find(g[v].begin(), g[v].end(), u));
    g[u].erase(find(g[u].begin(), g[u].end(), v));
    for (int vv = 0; vv < n; vv++) {
      for (int i = 0; i < sz(g[vv]); i++) {
        int uu = g[vv][i];
        g[n + vv].push_back(n + uu);
      }
    }
    g[v].push_back(n + u);
    g[n + u].push_back(v);
    g[u].push_back(n + v);
    g[n + v].push_back(u);
    int k = sz(c);
    for (int i = 0; i < k; i++) {
      c.push_back(n + c[i]);
    }
    n *= 2;
  }
  long long res = solveEven(c);
  assert(!(res & 1));
  res /= 2;
  printf("%lld\n", res);
}

int main() {
  precalc();
#ifdef DEBUG
  assert(freopen(TASK ".in", "r", stdin));
  assert(freopen(TASK ".out", "w", stdout));
#endif
  while (read()) {
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}


Measured memory_bytes: 15992