// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

#pragma GCC optimize("Ofast")

typedef long long lint;

typedef unsigned int uint;

typedef pair<int, int> pii;

typedef pair<lint, lint> pll;

typedef unsigned long long ulint;

#define endl '\n'

#define fst first

#define sed second

#define pb push_back

#define mp make_pair

#define rint register int

#define newline putchar('\n')

#define leave_space putchar(' ')

#define all(x) (x).begin(), (x).end()

#define reveal(x) cerr << #x << " = " << (x) << endl

#define rep(it, f, e) for (rint it = (f); it <= int(e); ++it)

#define per(it, f, e) for (rint it = (f); it >= int(e); --it)

const int MAXN = 1e5 + 10;

vector<int> edge[MAXN], cycle;

struct DSU {

	int par[MAXN];

	DSU() {

		rep (i, 1, MAXN - 1) {

			par[i] = i;

		}

	}

	inline int find(int x) {

		return par[x] == x ? x : par[x] = find(par[x]);

	}

	inline void marry(int x, int y) {

		par[find(x)] = find(y);

	}

}	Dsu;

int val[MAXN], par[MAXN];

inline void dfs(int u, int col) {

	val[u] = col;

	for (auto v : edge[u]) {

		if (v == par[u]) continue;

		par[v] = u;

		dfs(v, -col);

		val[u] += val[v];

	}

}

int main() {

	ios::sync_with_stdio(false);

	cin.tie(NULL), cout.tie(NULL);

	int le1 = 1, le2 = 1;

	int n, m, u, v;

	cin >> n >> m;

	rep (i, 1, m) {

		cin >> u >> v;

		if (Dsu.find(u) != Dsu.find(v)) {

			Dsu.marry(u, v);

			edge[u].pb(v);

			edge[v].pb(u);

		} else {

			le1 = u, le2 = v;

		}

	}

	dfs(le1, 1);

	if (n == m) {

		for (int u = le2; u; u = par[u]) {

			cycle.pb(val[u]);

		}

        if (cycle.size() & 1) {

			if (val[le1] & 1) {

				cout << -1 << endl;

				return 0;

			}

			for (int u = le2; u; u = par[u]) {

				val[u] -= val[le1] / 2;

			}

        } else {

			if (val[le1]) {

				cout << -1 << endl;

				return 0;

			}

			sort(all(cycle));

			for (int u = le2; u; u = par[u]) {

				val[u] -= cycle[cycle.size() / 2 - 1];

			}

        }

	} else if (val[le1]) {

		cout << -1 << endl;

		return 0;

	}

	lint ans = 0;

	rep (i, 1, n) {

		ans += abs(val[i]);

	}

    cout << ans << endl;

	return 0;

}

// Optimized solution to the same problem (faster):


#include<iostream>

#include<cmath>

#include<algorithm>

#include<cstdio>

using namespace std;



long long ans=0;

int tot=0,h[100005];

int n,m,x,y,S,T,k[100005],siz[100005];

int s[100005],top;

bool odd;

struct Edge{

    int x,next;

}e[200005];



inline void add_edge(int x,int y){

    e[++tot].x=y;

    e[tot].next=h[x],h[x]=tot;

}



void dfs1(int x,int fa){

    for(int i=h[x];i;i=e[i].next){

        if(e[i].x==fa)continue;

        if(siz[e[i].x]){

            if(siz[x]==siz[e[i].x])odd=true;

            S=x,T=e[i].x;

        }else{

            siz[e[i].x]=-siz[x];

            dfs1(e[i].x,x);

        }

    }

}



void dfs2(int x,int fa){

    for(int i=h[x];i;i=e[i].next){

        if(e[i].x==fa||(x==S&&e[i].x==T)||(x==T&&e[i].x==S))continue;

        dfs2(e[i].x,x);

        siz[x]+=siz[e[i].x];

        k[x]+=k[e[i].x];

    }

}



int main(){

    scanf("%d%d",&n,&m);

    for(int i=1;i<=m;i++){

        scanf("%d%d",&x,&y);

        add_edge(x,y);

        add_edge(y,x);

    }

    siz[1]=1,dfs1(1,0);

    int sum=0;

    for(int i=1;i<=n;i++)sum+=siz[i];

    if(m==n-1){

        if(sum)return printf("-1"),0;

    }else{

        if(odd){

            if(sum&1)return printf("-1"),0;

            ans+=abs(sum>>1);

            siz[S]-=sum>>1,siz[T]-=sum>>1;

        }else{

            if(sum)return printf("-1"),0;

            else k[S]=1,k[T]=-1;

        }

    }

    dfs2(1,0);

    for(int i=1;i<=n;i++){

        if(k[i])s[++top]=k[i]*siz[i];

        else ans+=abs(siz[i]);

    }

    s[++top]=0;

    sort(s+1,s+top+1);

    int mid=s[top+1>>1];

    for(int i=1;i<=top;i++)ans+=abs(s[i]-mid);

    printf("%lld",ans);

}

// Fastest known solution for this problem: