// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 4.0010069884138435x):
/*mjytxdy!*/

#include <bits/stdc++.h>

using namespace std;

#define ll long long

#define db double

#define ull unsigned long long

#define ldb long double

#define pb push_back

#define fir first

#define sec second

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

#define pii pair<int,int>

#define pil pair<int,ll>

#define pli pair<ll,int>

#define pll pair<ll,ll>

#define inl inline

#define rep(i,l,r) for(int i=(l);i<=(r);i++)

#define per(i,r,l) for(int i=(r);i>=(l);i--)

#define travel(i,u) for(int i=head[u];i;i=e[i].next)

#define lb(x) ((x)&(-(x)))

#define lson root<<1

#define rson root<<1|1

#define Mid (l+r>>1)

#define debug(x) cout<<#x":"<<x<<'\n'

#define myput puts("QaQ")

#define file(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);

const int MAXN = 1e5+10;

int n,m,u,v,x=1,y,cnt,vis[MAXN],sum[MAXN],fa[MAXN],head[MAXN],f[MAXN];

ll ans;

struct Edge{int to,next;}e[MAXN<<1];

inl char read(){

	static const int IO_LEN=1024*1024;

	static char buf[IO_LEN],*ioh,*iot;

	if(ioh==iot){

		iot=(ioh=buf)+fread(buf,1,IO_LEN,stdin);

		if(ioh==iot) return -1;

	}

	return *ioh++;

}

template<class T>inl void read(T &x){

	static int iosig;static char ioc;

	for(iosig=0,ioc=read();!isdigit(ioc);ioc=read()) if(ioc=='-') iosig=1;

	for(x=0;isdigit(ioc);ioc=read())x=(x<<1)+(x<<3)+(ioc^'0');

	if(iosig) x=-x;

}

inl void add(int u,int v){

	e[++cnt]=(Edge){v,head[u]},head[u]=cnt;

}

inl void dfs(int now,int dep,int father){

	sum[now]=dep&1?1:-1,fa[now]=father;

	travel(i,now){

		if(e[i].to==father) continue;

		dfs(e[i].to,dep+1,now);

		sum[now]+=sum[e[i].to];

	}

}

inl int find(int x){return f[x]==x?x:f[x]=find(f[x]);}

int main(){

//	freopen("in.txt","r",stdin);

	read(n),read(m);

	rep(i,1,n)f[i]=i;

	rep(i,1,m){

		read(u),read(v);

		if(find(u)!=find(v))add(u,v),add(v,u),f[find(u)]=find(v);

		else x=u,y=v;

	}

	dfs(x,1,0);

	if(m==n-1) {if(sum[x]) return puts("-1")&0;}

	else{

		vector<pii> mjy;int now=y;

		while(now) mjy.pb({sum[now],now}),now=fa[now];

		if(mjy.size()&1) {

			if(sum[x]&1) return puts("-1")&0;

			for(auto i:mjy)sum[i.sec]-=sum[x]>>1;

		}else {

			if(sum[x]) return puts("-1")&0;

			sort(all(mjy));

			int mid=mjy[mjy.size()>>1].fir;

			for(auto i:mjy)sum[i.sec]-=mid;

		}

	}

	rep(i,1,n)ans+=abs(sum[i]);

	printf("%lld\n",ans);

	return 0;

}