#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define unittest_name_helper(counter) unittest_ ## counter
#define unittest_name(counter) unittest_name_helper(counter)
#define unittest __attribute__((constructor)) void unittest_name(__COUNTER__) ()
using ll = long long;
using namespace std;

template <typename UnaryPredicate>
int64_t binsearch(int64_t l, int64_t r, UnaryPredicate p) {
    assert (l <= r);
    -- l;
    while (r - l > 1) {
        int64_t m = l + (r - l) / 2;
        (p(m) ? r : l) = m;
    }
    return r;
}

/**
 * @param g a simple connected undirected graph with |E| = |V|
 */
deque<int> get_namori_cycle(vector<vector<int> > const & g) {
    int n = g.size();
    { int m = 0; REP (i, n) m += g[i].size(); assert (m == 2 * n); }  // assume the namori-ty
    deque<int> stk;
    vector<bool> used(n);
    function<void (int, int)> go = [&](int i, int parent) {
        if (used[i]) throw i;
        stk.push_back(i);
        used[i] = true;
        for (int j : g[i]) if (j != parent) {
            go(j, i);
        }
        assert (stk.back() == i);
        stk.pop_back();
        used[i] = false;
    };
    try {
        go(0, -1);
        assert (false);  // fails if the graph is not simple
    } catch (int i) {
        while (stk.front() != i) {
            stk.pop_front();
        }
    }
    return stk;
}
unittest {
    vector<vector<int> > g(10);
    auto add_edge = [&](int i, int j) {
        g[i].push_back(j);
        g[j].push_back(i);
    };
    add_edge(0, 5);
    add_edge(1, 7);
    add_edge(2, 7);
    add_edge(3, 8);
    add_edge(4, 5);
    add_edge(5, 9);
    add_edge(6, 7);
    add_edge(7, 8);
    add_edge(8, 9);
    add_edge(3, 1);
    assert (get_namori_cycle(g) == deque<int>({ 8, 3, 1, 7 }));
}
unittest {
    vector<vector<int> > g(10);
    auto add_edge = [&](int i, int j) {
        g[i].push_back(j);
        g[j].push_back(i);
    };
    add_edge(0, 1);
    add_edge(1, 2);
    add_edge(1, 3);
    add_edge(2, 4);
    add_edge(3, 5);
    add_edge(4, 6);
    add_edge(6, 2);
    add_edge(6, 7);
    add_edge(7, 8);
    add_edge(8, 9);
    assert (get_namori_cycle(g) == deque<int>({ 2, 4, 6 }));
}

pair<ll, int> solve_tree(int i, vector<vector<int> > const & g, vector<bool> & used) {
    assert (used[i]);
    ll acc = 0;
    int delta = 1;
    for (int j : g[i]) if (not used[j]) {
        used[j] = true;
        auto it = solve_tree(j, g, used);
        acc += it.first;
        acc += abs(it.second);
        delta -= it.second;
    }
    return make_pair(acc, delta);
}

ll solve_path(deque<int> const & xs) {
    ll acc = 0;
    int delta = 0;
    for (int x : xs) {
        acc += abs(delta);
        delta *= -1;
        delta += x;
    }
    assert (not delta);
    return acc;
}

ll solve_cycle(deque<int> xs) {
    int sum_x = 0;
    int sum_abs_x = 0;
    for (int x : xs) {
        sum_x *= -1;
        sum_x += x;
        sum_abs_x += abs(x);
    }
    auto func = [&](int k) {
        xs.front() += k;
        xs.back()  += k;
        ll value = solve_path(xs);
        xs.front() -= k;
        xs.back()  -= k;
        return value;
    };

    if (xs.size() % 2 == 0) {
        if (sum_x) return -1;
        int k = binsearch(- sum_abs_x, sum_abs_x + 1, [&](int k) {
            return func(k + 1) - func(k) >= 0;
        });
        return abs(k) + func(k);

    } else {
        if (sum_x % 2) return -1;
        int k = - sum_x / 2;
        return abs(k) + func(k);
    }
}

ll solve(int n, int m, vector<vector<int> > const & g) {
    vector<bool> used(n);
    if (m == n - 1) {
        constexpr int root = 0;
        used[root] = true;
        auto it = solve_tree(root, g, used);
        if (it.second) return -1;
        return it.first;

    } else if (m == n) {
        deque<int> namori = get_namori_cycle(g);
        for (int i : namori) {
            used[i] = true;
        }
        ll acc = 0;
        deque<int> delta;
        for (int root : namori) {
            auto it = solve_tree(root, g, used);
            acc += it.first;
            delta.push_back(it.second);
        }
        ll acc1 = solve_cycle(delta);
        if (acc1 == -1) return -1;
        return acc + acc1;

    } else {
        assert (false);
    }
}

int main() {
    int n, m; scanf("%d%d", &n, &m);
    vector<vector<int> > g(n);
    REP (i, m) {
        int a, b; scanf("%d%d", &a, &b);
        -- a; -- b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    ll answer = solve(n, m, g);
    printf("%lld\n", answer);
    return 0;
}


Measured memory_bytes: 13312