#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>

#define pb push_back
#define mp make_pair

#define f first
#define s second

#define pii pair<int, int>

using namespace std;

const int N = (int) 2e5 + 10;
const int MOD = (int) 1e9 + 7;

int n;
vector<int> g[N];

int dp[N];
int up[N];
int batya[N];

int combine(int a, int b) {
  if (a == 0 || b == 0)
    return a ? a : b;
  if (a == -1 || b == -1 || a != b)
    return -1;
  assert(a == b);
  return a;
}

void dfs(int v, int parent = 0) {
  batya[v] = parent;
  for (auto to : g[v]) {
    if (to == parent) continue;
    dfs(to, v);
    dp[v] = combine(dp[v], dp[to]);
  }
  if (dp[v] != -1) ++dp[v];
}

void calc_up(int v, int parent = 0) {
  vector<int> children;
  for (auto to : g[v]) {
    if (to == parent) continue;
    children.pb(to);
  }
  if (children.empty())
    return;
  vector<int> prefix;
  prefix.resize(children.size());
  for (int i = 0; i < prefix.size(); i++) {
    if (i) prefix[i] = prefix[i - 1];
    prefix[i] = combine(prefix[i], dp[children[i]]);
  }
  int suffix = 0;
  for (int i = (int) children.size() - 1; i >= 0; i--) {
    int to = children[i];
    int we = combine(suffix, i ? prefix[i - 1] : 0);
    up[to] = combine(we, up[v]);
    if (up[to] != -1) up[to]++;
    calc_up(to, v);
    suffix = combine(suffix, dp[children[i]]);
  }
}

int main() {
  #ifdef LOCAL
  freopen("in.txt", "r", stdin);
  //freopen("out", "w", stdout);
  #endif

  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);
  calc_up(1);
  //for (int i = 1; i <= n; i++)
  //  cout << dp[i] << ' ';
  //cout << endl;
  //for (int i = 1; i <= n; i++)
  //  cout << up[i] << ' ';
  //cout << endl;
  //for (int i = 1; i <= n; i++)
  //  if (up[i] != -1) --up[i];
  int ans = MOD;
  for (int root = 1; root <= n; root++) {
    vector<int> deti;
    for (auto to : g[root]) {
      if (to == batya[root]) continue;
      deti.pb(dp[to]);
    }
    if (up[root])
      deti.pb(up[root]);
    sort(deti.begin(), deti.end());
    deti.resize(unique(deti.begin(), deti.end()) - deti.begin());
    if (deti.size() > 2) continue;
    if (deti.empty()) continue;
    if (deti[0] == -1) continue;
    int sum = 0;
    for (auto it : deti) sum += it;
    ans = min(ans, sum);
  }
  if (ans == MOD) ans = -1;
  cout << ans;
  return 0;
}


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>

#define pb push_back
#define mp make_pair

#define f first
#define s second

#define pii pair<int, int>

using namespace std;

const int N = (int) 2e5 + 10;
const int MOD = (int) 1e9 + 7;

int n;
vector<int> g[N];

int dp[N];
int up[N];
int batya[N];

int combine(int a, int b) {
  if (a == 0 || b == 0)
    return a ? a : b;
  if (a == -1 || b == -1 || a != b)
    return -1;
  assert(a == b);
  return a;
}

void dfs(int v, int parent = 0) {
  batya[v] = parent;
  for (auto to : g[v]) {
    if (to == parent) continue;
    dfs(to, v);
    dp[v] = combine(dp[v], dp[to]);
  }
  if (dp[v] != -1) ++dp[v];
}

void calc_up(int v, int parent = 0) {
  vector<int> children;
  for (auto to : g[v]) {
    if (to == parent) continue;
    children.pb(to);
  }
  if (children.empty())
    return;
  vector<int> prefix;
  prefix.resize(children.size());
  for (int i = 0; i < prefix.size(); i++) {
    if (i) prefix[i] = prefix[i - 1];
    prefix[i] = combine(prefix[i], dp[children[i]]);
  }
  int suffix = 0;
  for (int i = (int) children.size() - 1; i >= 0; i--) {
    int to = children[i];
    int we = combine(suffix, i ? prefix[i - 1] : 0);
    up[to] = combine(we, up[v]);
    if (up[to] != -1) up[to]++;
    calc_up(to, v);
    suffix = combine(suffix, dp[children[i]]);
  }
}

int main() {
  #ifdef LOCAL
  freopen("in.txt", "r", stdin);
  //freopen("out", "w", stdout);
  #endif

  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);
  calc_up(1);
  //for (int i = 1; i <= n; i++)
  //  cout << dp[i] << ' ';
  //cout << endl;
  //for (int i = 1; i <= n; i++)
  //  cout << up[i] << ' ';
  //cout << endl;
  //for (int i = 1; i <= n; i++)
  //  if (up[i] != -1) --up[i];
  int ans = MOD;
  for (int root = 1; root <= n; root++) {
    vector<int> deti;
    for (auto to : g[root]) {
      if (to == batya[root]) continue;
      deti.pb(dp[to]);
    }
    if (up[root])
      deti.pb(up[root]);
    sort(deti.begin(), deti.end());
    deti.resize(unique(deti.begin(), deti.end()) - deti.begin());
    if (deti.size() > 2) continue;
    if (deti.empty()) continue;
    if (deti[0] == -1) continue;
    int sum = 0;
    for (auto it : deti) sum += it;
    ans = min(ans, sum);
  }
  if (ans == MOD) {
    cout << -1;
    return 0;
  }
  while (ans % 2 == 0) ans /= 2;
  cout << ans;
  return 0;
}


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>

#define pb push_back
#define mp make_pair

#define f first
#define s second

#define pii pair<int, int>

using namespace std;

const int N = (int) 2e5 + 10;
const int MOD = (int) 1e9 + 7;

int n;
vector<int> g[N];

int dp[N];
int up[N];
int batya[N];

int combine(int a, int b) {
  if (a == 0 || b == 0)
    return a ? a : b;
  if (a == -1 || b == -1 || a != b)
    return -1;
  assert(a == b);
  return a;
}

void dfs(int v, int parent = 0) {
  batya[v] = parent;
  for (auto to : g[v]) {
    if (to == parent) continue;
    dfs(to, v);
    dp[v] = combine(dp[v], dp[to]);
  }
  if (dp[v] != -1) ++dp[v];
}

void calc_up(int v, int parent = 0) {
  vector<int> children;
  for (auto to : g[v]) {
    if (to == parent) continue;
    children.pb(to);
  }
  if (children.empty())
    return;
  vector<int> prefix;
  prefix.resize(children.size());
  for (int i = 0; i < prefix.size(); i++) {
    if (i) prefix[i] = prefix[i - 1];
    prefix[i] = combine(prefix[i], dp[children[i]]);
  }
  int suffix = 0;
  for (int i = (int) children.size() - 1; i >= 0; i--) {
    int to = children[i];
    int we = combine(suffix, i ? prefix[i - 1] : 0);
    up[to] = combine(we, up[v]);
    if (up[to] != -1) up[to]++;
    calc_up(to, v);
    suffix = combine(suffix, dp[children[i]]);
  }
}

int main() {
  #ifdef LOCAL
  freopen("in.txt", "r", stdin);
  //freopen("out", "w", stdout);
  #endif

  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);
  calc_up(1);
  //for (int i = 1; i <= n; i++)
  //  cout << dp[i] << ' ';
  //cout << endl;
  //for (int i = 1; i <= n; i++)
  //  cout << up[i] << ' ';
  //cout << endl;
  //for (int i = 1; i <= n; i++)
  //  if (up[i] != -1) --up[i];
  int ans = MOD;
  for (int root = 1; root <= n; root++) {
    vector<int> deti;
    for (auto to : g[root]) {
      if (to == batya[root]) continue;
      deti.pb(dp[to]);
    }
    deti.pb(up[root]);
    sort(deti.begin(), deti.end());
    deti.resize(unique(deti.begin(), deti.end()) - deti.begin());
    if (deti.size() > 2) continue;
    if (deti.empty()) continue;
    if (deti[0] == -1) continue;
    //cout<<root<<" -> ";
    //for(auto it : deti) cout << it << " ";cout<<endl;
    int sum = 0;
    for (auto it : deti) sum += it;
    ans = min(ans, sum);
  }
  if (ans == MOD) ans = -1;
  cout << ans;
  return 0;
}


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>

#define pb push_back
#define mp make_pair

#define f first
#define s second

#define pii pair<int, int>

using namespace std;

const int N = (int) 2e5 + 10;
const int MOD = (int) 1e9 + 7;

int n;
vector<int> g[N];

int dp[N];
int up[N];
int batya[N];

int combine(int a, int b) {
  if (a == 0 || b == 0)
    return a ? a : b;
  if (a == -1 || b == -1 || a != b)
    return -1;
  assert(a == b);
  return a;
}

void dfs(int v, int parent = 0) {
  batya[v] = parent;
  for (auto to : g[v]) {
    if (to == parent) continue;
    dfs(to, v);
    dp[v] = combine(dp[v], dp[to]);
  }
  if (dp[v] != -1) ++dp[v];
}

void calc_up(int v, int parent = 0) {
  vector<int> children;
  for (auto to : g[v]) {
    if (to == parent) continue;
    children.pb(to);
  }
  if (children.empty())
    return;
  vector<int> prefix;
  prefix.resize(children.size());
  for (int i = 0; i < prefix.size(); i++) {
    if (i) prefix[i] = prefix[i - 1];
    prefix[i] = combine(prefix[i], dp[children[i]]);
  }
  int suffix = 0;
  for (int i = (int) children.size() - 1; i >= 0; i--) {
    int to = children[i];
    int we = combine(suffix, i ? prefix[i - 1] : 0);
    up[to] = combine(we, up[v]);
    if (up[to] != -1) up[to]++;
    calc_up(to, v);
    suffix = combine(suffix, dp[children[i]]);
  }
}

int main() {
  #ifdef LOCAL
  freopen("in.txt", "r", stdin);
  //freopen("out", "w", stdout);
  #endif

  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1);
  calc_up(1);
  //for (int i = 1; i <= n; i++)
  //  cout << dp[i] << ' ';
  //cout << endl;
  //for (int i = 1; i <= n; i++)
  //  cout << up[i] << ' ';
  //cout << endl;
  //for (int i = 1; i <= n; i++)
  //  if (up[i] != -1) --up[i];
  int ans = MOD;
  for (int root = 1; root <= 1; root++) {
    vector<int> deti;
    for (auto to : g[root]) {
      if (to == batya[root]) continue;
      deti.pb(dp[to]);
    }
    if (up[root])
      deti.pb(up[root]);
    sort(deti.begin(), deti.end());
    deti.resize(unique(deti.begin(), deti.end()) - deti.begin());
    if (deti.size() > 2) continue;
    if (deti.empty()) continue;
    if (deti[0] == -1) continue;
    int sum = 0;
    for (auto it : deti) sum += it;
    ans = min(ans, sum);
  }
  if (ans == MOD) ans = -1;
  cout << ans;
  return 0;
}


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>

#define pb push_back
#define mp make_pair

#define f first
#define s second

#define pii pair<int, int>

using namespace std;

const int N = (int) 2e5 + 10;
const int MOD = (int) 1e9 + 7;

int n;
vector<int> g[N];

int combine(int a, int b) {
  if (a == 0 || b == 0)
    return a ? a : b;
  if (a == -1 || b == -1 || a != b)
    return -1;
  assert(a == b);
  return a;
}

int dfs(int v, int parent = 0) {
  int res = 0;
  for (auto to : g[v]) {
    if (to == parent) continue;
    res = combine(res, dfs(to, v));
  }
  if (res != -1) ++res;
  return res;
}

int main() {
  #ifdef LOCAL
  freopen("in.txt", "r", stdin);
  //freopen("out", "w", stdout);
  #endif

  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].pb(v);
    g[v].pb(u);
  }
  int ans = MOD;
  for (int i = 1; i <= n; i++) {
    int cur = dfs(i);
    if (cur != -1)
      ans = min(ans, cur);
  }
  if (ans == MOD) {
    ans = 0;
  }
  cout << ans - 1;
  return 0;
}


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>

#define pb push_back
#define mp make_pair

#define f first
#define s second

#define pii pair<int, int>

using namespace std;

const int N = (int) 2e5 + 10;
const int MOD = (int) 1e9 + 7;

int n;
vector<int> g[N];

int combine(int a, int b) {
  if (a == 0 || b == 0)
    return a ? a : b;
  if (a == -1 || b == -1 || a != b)
    return -1;
  assert(a == b);
  return a;
}

int dfs(int v, int parent = 0) {
  if (!parent) {
    vector<int> ch;
    for (auto to : g[v]) {
      if (to == parent) continue;
      ch.pb(dfs(to, v));
    }
    sort(ch.begin(), ch.end());
    ch.resize(unique(ch.begin(), ch.end()) - ch.begin());
    int res = 0;
    for (auto x : ch) {
      if (x == -1) {
        res = -1;
        break;
      }
      res += x;
    }
    if (res != -1 && ch.size() <= 2) return res + 1;
    return -1;
  }
  int res = 0;
  for (auto to : g[v]) {
    if (to == parent) continue;
    res = combine(res, dfs(to, v));
  }
  if (res != -1) ++res;
  return res;
}

int main() {
  #ifdef LOCAL
  freopen("in.txt", "r", stdin);
  //freopen("out", "w", stdout);
  #endif

  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].pb(v);
    g[v].pb(u);
  }
  int ans = MOD;
  for (int i = 1; i <= n; i++) {
    int cur = dfs(i);
    if (cur != -1)
      ans = min(ans, cur);
  }
  if (ans == MOD) {
    ans = 0;
  }
  cout << ans - 1;
  return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
const int maxn = 2*100000+5;
vector<int> gra[maxn];
int n;
bool vis[maxn];
int dis[maxn];
int root;
set<int>::iterator it;
int dfs(int x)
{
	set<int> s;
	vis[x] = 1;
	for(int i = 0; i < gra[x].size(); i++){
		int to = gra[x][i];
		if (!vis[to]){
			int tmp = dfs(to);
			//printf("%d %d %d\n",to,tmp,dis[to]);
			if (tmp == -1) return -1;
			else if (tmp == 1 || tmp == 0){
				s.insert(dis[to]);
			}
			else if (tmp == 2){
				root = to;
				return -1;
		    }
			else {
				root = -1;
				return -1;
			}
		}
	}
	int ans = 0;
	if (x == root && s.size() == 2){
		//printf("!%d",x);
		for(it = s.begin(); it != s.end(); it++){
			ans += *it;
		}
		return ans;
	}
	else if (x == root && s.size() == 1){
		return (*s.begin());
	}
	else if (x == root){
		//printf(".%d",x);
		return -1;
    }
	if (s.size() == 1) dis[x] = *s.begin() + 1;
	else if (s.size() == 0) dis[x] = 1;
	return s.size();
}

int main()
{
	scanf("%d",&n);
	for(int i = 0; i < n - 1; i ++){
		int x,y;
		scanf("%d %d",&x,&y);
		gra[x].push_back(y);
		gra[y].push_back(x);
	}
 	memset(vis,0,sizeof(vis));
 	memset(dis,0,sizeof(dis));
 	root = 1;
 	int ans;
 	int tmp = dfs(root);
 	if (tmp == -1 && root == 1) printf("-1\n");
 	else if (tmp == -1 && root != 1){
 		memset(vis,0,sizeof(vis));
 		memset(dis,0,sizeof(dis));
 		int tmp2 = dfs(root);
 		if (tmp2 == -1) printf("-1\n");
 		else {
 			while(tmp2 % 2 == 0)
 				tmp2 = tmp2/2;
 			printf("%d\n",tmp2);
		 }
	 }
	 else {
	 	while(tmp % 2 == 0)
	 		tmp = tmp/2;
	 	printf("%d\n",tmp);
	 }
	 return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
const int maxn = 4 * 100000+5;
struct edge{
	int from;
	int to;
}tree[maxn];
vector<int> gra[maxn];
int n;
int dp[maxn];
set<int>::iterator it;

int dfs(int x)
{
	if (dp[x] != -1) return dp[x];
	int cur = tree[x].to;
	set<int> s;
	for(int i = 0; i < gra[cur].size(); i++){
		int e = gra[cur][i];
		if (tree[e].to == tree[x].from) continue;
		dfs(e);
		if (dp[e] == -1) return (dp[x] = -1);
		s.insert(dfs(e));
		if (s.size() >= 2) {
			return (dp[x] = -1);
		}
	}
	if (s.size() == 0) return (dp[x] = 1);
	else return (dp[x] = 1 + *s.begin());
}

int solve()
{
	for(int i = 0; i <= 2*(n - 2) + 1; i++){
		dfs(i);
		//printf("from %d to %d %d\n",tree[i].from,tree[i].to,dfs(i));
	}
	return 0;
}

int main()
{
	memset(dp,-1,sizeof(dp));
	scanf("%d",&n);
	for(int i = 0; i < n - 1; i++){
		int x,y;
		scanf("%d %d",&x,&y);
	 	tree[2*i].from = x;
	 	tree[2*i].to = y;
	 	gra[x].push_back(2*i);
	 	tree[2*i+1].from = y;
	 	tree[2*i+1].to = x;
	 	gra[y].push_back(2*i + 1);
	}
	solve();
	int ans = 0;
	for(int i = 1; i <= n; i++){
		set<int> s;
		int ok = 1;
		for(int j = 0; j < gra[i].size();j++){
			int e = gra[i][j];
			//printf("%d %d %d\n",i,tree[e].to,dp[e]);
			if (dp[e] == -1) {
				ok = 0;
				break;
			}
			s.insert(dp[e]);
			if (s.size() > 2) {
				ok = 0;
				break;
			}
		}
		if (!ok) continue;
		else if (s.size() == 2){
			for(it = s.begin(); it != s.end(); it++){
				ans +=  *it;
			}
			break;
		}
		else if (s.size() == 1){
			ans = *s.begin();
			break;
		}
	}
    if (ans == 0) {
		printf("-1\n");
		return 0;
    }
    while(ans % 2 == 0){
    	ans = ans / 2;
	}
	printf("%d\n",ans);
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
const int maxn = 4 * 100000+5;
struct edge{
	int from;
	int to;
}tree[maxn];
vector<int> gra[maxn];
int n;
int dp[maxn];
set<int>::iterator it;

int dfs(int x)
{
	if (dp[x] != -1) return dp[x];
	int cur = tree[x].to;
	set<int> s;
	for(int i = 0; i < gra[cur].size(); i++){
		int e = gra[cur][i];
		if (tree[e].to == tree[x].from) continue;
		s.insert(dfs(e));
		if (s.size() >= 2) {
			return (dp[x] = -1);
		}
	}
	if (s.size() == 0) return (dp[x] = 1);
	else return (dp[x] = 1 + *s.begin());
}

int solve()
{
	for(int i = 0; i <= 2*(n - 2) + 1; i++){
		dfs(i);
		//printf("from %d to %d %d\n",tree[i].from,tree[i].to,dfs(i));
	}
	return 0;
}

int main()
{
	memset(dp,-1,sizeof(dp));
	scanf("%d",&n);
	for(int i = 0; i < n - 1; i++){
		int x,y;
		scanf("%d %d",&x,&y);
	 	tree[2*i].from = x;
	 	tree[2*i].to = y;
	 	gra[x].push_back(2*i);
	 	tree[2*i+1].from = y;
	 	tree[2*i+1].to = x;
	 	gra[y].push_back(2*i + 1);
	}
	solve();
	int ans = 0;
	for(int i = 1; i <= n; i++){
		set<int> s;
		int ok = 1;
		for(int j = 0; j < gra[i].size();j++){
			int e = gra[i][j];
			//printf("%d %d %d\n",i,tree[e].to,dp[e]);
			if (dp[e] == -1) {
				ok = 0;
				break;
			}
			s.insert(dp[e]);
			if (s.size() > 2) {
				ok = 0;
				break;
			}
		}
		if (!ok) continue;
		else if (s.size() == 2){
			for(it = s.begin(); it != s.end(); it++){
				ans +=  *it;
			}
			break;
		}
		else if (s.size() == 1){
			ans = *s.begin();
			break;
		}
	}
    if (ans == 0) {
		printf("-1\n");
		return 0;
    }
    while(ans % 2 == 0){
    	ans = ans / 2;
	}
	printf("%d\n",ans);
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
const int maxn = 100000+5;
vector<int> gra[maxn];
int n;
bool vis[maxn];
int dis[maxn];
int root;
set<int>::iterator it;
int dfs(int x)
{
	set<int> s;
	vis[x] = 1;
	for(int i = 0; i < gra[x].size(); i++){
		int to = gra[x][i];
		if (!vis[to]){
			int tmp = dfs(to);
			if (tmp == -1) return -1;
			else if (tmp == 1 || tmp == 0){
				s.insert(dis[to]);
			}
			else if (tmp == 2){
				root = to;
				return -1;
		    }
			else {
				root = -1;
				return -1;
			}
		}
	}
	int ans = 0;
	if (x == root && s.size() == 2){
		for(it = s.begin(); it != s.end(); it++){
			ans += *it;
		}
		return ans;
	}
	else if (x == root && s.size() == 1){
		return *s.begin();
	}
	else if (x == root) return -1;
	if (s.size() == 1) dis[x] = *s.begin() + 1;
	else if (s.size() == 0) dis[x] = 1;
	return s.size();
}

int main()
{
	scanf("%d",&n);
	for(int i = 0; i < n - 1; i ++){
		int x,y;
		scanf("%d %d",&x,&y);
		gra[x].push_back(y);
		gra[y].push_back(x);
	}
 	memset(vis,0,sizeof(vis));
 	memset(dis,0,sizeof(dis));
 	root = 1;
 	dfs(1);
 	int ans;
 	int tmp = dfs(root);
 	if (tmp == -1 && root == 1) printf("-1\n");
 	else if (tmp == -1 && root != 1){
 		memset(vis,0,sizeof(vis));
 		memset(dis,0,sizeof(dis));
 		int tmp2 = dfs(root);
 		if (tmp2 == -1) printf("-1\n");
 		else {
 			while(tmp2 % 2 == 0)
 				tmp2 = tmp2/2;
 			printf("%d\n",tmp2);
		 }
	 }
	 else {
	 	while(tmp % 2 == 0)
	 		tmp = tmp/2;
	 	printf("%d\n",tmp);
	 }
	 return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
const int maxn = 100000+5;
vector<int> gra[maxn];
int n;
bool vis[maxn];
int dis[maxn];
int root;
set<int>::iterator it;
int dfs(int x)
{
	set<int> s;
	vis[x] = 1;
	for(int i = 0; i < gra[x].size(); i++){
		int to = gra[x][i];
		if (!vis[to]){
			int tmp = dfs(to);
			//printf("%d %d %d\n",to,tmp,dis[to]);
			if (tmp == -1) return -1;
			else if (tmp == 1 || tmp == 0){
				s.insert(dis[to]);
			}
			else if (tmp == 2){
				root = to;
				return -1;
		    }
			else {
				root = -1;
				return -1;
			}
		}
	}
	int ans = 0;
	if (x == root && s.size() == 2){
		//printf("!%d",x);
		for(it = s.begin(); it != s.end(); it++){
			ans += *it;
		}
		return ans;
	}
	else if (x == root && s.size() == 1){
		return (*s.begin());
	}
	else if (x == root){
		//printf(".%d",x);
		return -1;
    }
	if (s.size() == 1) dis[x] = *s.begin() + 1;
	else if (s.size() == 0) dis[x] = 1;
	return s.size();
}

int main()
{
	scanf("%d",&n);
	for(int i = 0; i < n - 1; i ++){
		int x,y;
		scanf("%d %d",&x,&y);
		gra[x].push_back(y);
		gra[y].push_back(x);
	}
 	memset(vis,0,sizeof(vis));
 	memset(dis,0,sizeof(dis));
 	root = 1;
 	int ans;
 	int tmp = dfs(root);
 	if (tmp == -1 && root == 1) printf("-1\n");
 	else if (tmp == -1 && root != 1){
 		memset(vis,0,sizeof(vis));
 		memset(dis,0,sizeof(dis));
 		int tmp2 = dfs(root);
 		if (tmp2 == -1) printf("-1\n");
 		else {
 			while(tmp2 % 2 == 0)
 				tmp2 = tmp2/2;
 			printf("%d\n",tmp2);
		 }
	 }
	 else {
	 	while(tmp % 2 == 0)
	 		tmp = tmp/2;
	 	printf("%d\n",tmp);
	 }
	 return 0;
}


#include <bits/stdc++.h>

#define ff first
#define ss second
#define mp make_pair

using namespace std;

typedef long long ll;

set<int> G[200005];
int st[200005], n, ans;
bool deuruim;

int val[2], qual_ponta, diametro, ponta[2];
int dist[2][200005];

void dia(int v, int p, int d){
	diametro = max(diametro, d);

	if(d > val[qual_ponta]){
		val[qual_ponta] = d;
		ponta[qual_ponta] = v;
	}

	for(int u : G[v]){
		if(p != u) dia(u,v,d+1);
	}
}

void dia2(int v, int p, int d, int tp){
	dist[tp][v] = d;

	for(int u : G[v]){
		if(p != u) dia2(u,v,d+1,tp);
	}
}

void calc(int v, int p){
	st[v] = 1;
	for(int x : G[v]){
		if(x != p){
			calc(x, v);
			st[v] += st[x];
		}
	}
}

bool dfs(int v, int p){
	int filhos = 0;
	bool flag = 0;
	map<int, vector<int> > tab;
	st[v] = 1;

	for(int x : G[v]){
		if(x == p) continue;

		filhos++;
		bool aux = dfs(x,v);
		flag |= aux;
		st[v] += st[x];

		if(!aux){
			tab[st[x]].push_back(x);
		}
	}

	for(auto &it : tab){
		while((it.ss).size() > 1){
			G[v].erase((it.ss).back());
			st[v] -= st[(it.ss).back()];
			filhos--;
			// n -= it.ff;
			(it.ss).pop_back();
		}
	}

	return flag | (filhos >= 2);
}

void check(int v, int p){
	// printf(". %d %d: %d\n",p,v,st[v]);
	ans++;
	if(G[v].size() > 2) deuruim = 1;

	for(int x : G[v])
		if(x != p) check(x,v);
}

int main(){
	int a,b;

	scanf("%d", &n);

	for(int i = 1; i < n; i++){
		scanf("%d%d", &a, &b);
		G[a].insert(b);
		G[b].insert(a);
	}

	diametro = 0;

	val[0] = val[1] = -1;
	qual_ponta = 0;
	dia(1,0,0);
	qual_ponta = 1;
	dia(ponta[0],0,0);

	dia2(ponta[0],0,0,0);
	dia2(ponta[1],0,0,1);

	int root = -1;

	for(int i = 1; i <= n; i++){
		if((diametro+1)/2 >= dist[0][i] && (diametro+1)/2 >= dist[1][i])
			root = i;
	}

	printf("%d %d %d\n", root, ponta[0], ponta[1]);

	dfs(root, root);

	ans = 0;
	check(root, root);

	if(deuruim) printf("-1\n");
	else{
		ans--;
		while(ans && ans%2 == 0) ans /= 2;
		printf("%d\n", ans);
	}

	return 0;
}


#include <bits/stdc++.h>

#define ff first
#define ss second
#define mp make_pair

using namespace std;

typedef long long ll;

set<int> G[200005];
int st[200005], n, ans;
bool deuruim;

void calc(int v, int p){
	st[v] = 1;
	for(int x : G[v]){
		if(x != p){
			calc(x, v);
			st[v] += st[x];
		}
	}
}

int find_centroid(int v, int p){
	for(int x : G[v]){
		if(x == p) continue;
		if(st[x]*2 > n) return find_centroid(x,v);
	}
	return v;
}

bool dfs(int v, int p){
	int filhos = 0;
	bool flag = 0;
	map<int, vector<int> > tab;

	for(int x : G[v]){
		if(x == p) continue;

		filhos++;
		bool aux = dfs(x,v);
		flag |= aux;

		if(!aux){
			tab[st[x]].push_back(x);
		}
	}

	for(auto &it : tab){
		while((it.ss).size() > 1){
			G[v].erase((it.ss).back());
			st[v] -= st[(it.ss).back()];
			filhos--;
			(it.ss).pop_back();
		}
	}

	return flag | (filhos >= 2);
}

void check(int v, int p){
	ans++;
	if(G[v].size() > 2) deuruim = 1;

	for(int x : G[v])
		if(x != p) check(x,v);
}

int main(){
	int a,b;

	scanf("%d", &n);

	for(int i = 1; i < n; i++){
		scanf("%d%d", &a, &b);
		G[a].insert(b);
		G[b].insert(a);
	}

	calc(1,1);
	int root = find_centroid(1,1);
	calc(root,root);

	dfs(root, root);

	ans = 0;
	check(root, root);

	if(deuruim) printf("-1\n");
	else printf("%d\n", ans-1);

	return 0;
}


#include <bits/stdc++.h>

#define ff first
#define ss second
#define mp make_pair

using namespace std;

typedef long long ll;

set<int> G[200005];
int st[200005], n, ans;
bool deuruim;

void calc(int v, int p){
	st[v] = 1;
	for(int x : G[v]){
		if(x != p){
			calc(x, v);
			st[v] += st[x];
		}
	}
}

int find_centroid(int v, int p){
	for(int x : G[v]){
		if(x == p) continue;
		if(st[x]*2 > n) return find_centroid(x,v);
	}
	return v;
}

bool dfs(int v, int p){
	int filhos = 0;
	bool flag = 0;
	map<int, vector<int> > tab;

	for(int x : G[v]){
		if(x == p) continue;

		filhos++;
		bool aux = dfs(x,v);
		flag |= aux;

		if(!aux){
			tab[st[x]].push_back(x);
		}
	}

	for(auto &it : tab){
		while((it.ss).size() > 1){
			G[v].erase((it.ss).back());
			st[v] -= st[(it.ss).back()];
			filhos--;
			(it.ss).pop_back();
		}
	}

	return flag | (filhos >= 2);
}

void check(int v, int p){
	ans++;
	if(G[v].size() > 2) deuruim = 1;

	for(int x : G[v])
		if(x != p) check(x,v);
}

int main(){
	int a,b;

	scanf("%d", &n);

	for(int i = 1; i < n; i++){
		scanf("%d%d", &a, &b);
		G[a].insert(b);
		G[b].insert(a);
	}

	calc(1,1);
	int root = find_centroid(1,1);
	calc(root,root);

	dfs(root, root);

	ans = 0;
	check(root, root);

	if(deuruim) printf("-1\n");
	else{
		ans--;
		while(ans && ans%2 == 0) ans /= 2;
		printf("%d\n", ans);
	}

	return 0;
}


#include <bits/stdc++.h>

#define ff first
#define ss second
#define mp make_pair

using namespace std;

typedef long long ll;

set<int> G[200005];
int st[200005], n, ans;
bool deuruim;

void calc(int v, int p){
	st[v] = 1;
	for(int x : G[v]){
		if(x != p){
			calc(x, v);
			st[v] += st[x];
		}
	}
}

int find_centroid(int v, int p){
	for(int x : G[v]){
		if(x == p) continue;
		if(st[x]*2 > n) return find_centroid(x,v);
	}
	return v;
}

bool dfs(int v, int p){
	int filhos = 0;
	bool flag = 0;
	map<int, vector<int> > tab;

	for(int x : G[v]){
		if(x == p) continue;

		filhos++;
		bool aux = dfs(x,v);
		flag |= aux;

		if(!aux){
			tab[st[x]].push_back(x);
		}
	}

	for(auto &it : tab){
		while((it.ss).size() > 1){
			G[v].erase((it.ss).back());
			st[v] -= it.ss;
			filhos--;
			n -= it.ss;
			(it.ss).pop_back();
		}
	}

	return flag | (filhos >= 2);
}

void check(int v, int p){
	ans++;
	if(G[v].size() > 2) deuruim = 1;

	for(int x : G[v])
		if(x != p) check(x,v);
}

int main(){
	int a,b;

	scanf("%d", &n);

	for(int i = 1; i < n; i++){
		scanf("%d%d", &a, &b);
		G[a].insert(b);
		G[b].insert(a);
	}

	int root = 1;
	int last = n;
	while(1){
		calc(root,root);
		root = find_centroid(root,root);
		calc(root,root);

		dfs(root, root);

		if(last == n) break;
		last = n;
	}

	ans = 0;
	check(root, root);

	if(deuruim) printf("-1\n");
	else{
		ans--;
		while(ans && ans%2 == 0) ans /= 2;
		printf("%d\n", ans);
	}

	return 0;
}


#include <bits/stdc++.h>

#define ff first
#define ss second
#define mp make_pair

using namespace std;

typedef long long ll;

set<int> G[200005];
int st[200005], dg[200005], n, ans;
bool deuruim;

int find_center(){
	queue<int> Q;
	bool vis[200005] = {0};

	for(int i = 1; i <= n; i++)
		if(dg[i] == 1){
			Q.push(i);
			vis[i] = 1;
		}

	while(!Q.empty()){
		int v = Q.front(); Q.pop();

		if(dg[v] == 0) return v;

		for(int x : G[v]){
			dg[x]--;
			if(!vis[x]){
				if(dg[x] == 1){
					Q.push(x);
					vis[x] = 1;
				}
			}
		}
	}

	return -1;
}

bool dfs(int v, int p){
	int filhos = 0;
	bool flag = 0;
	map<int, vector<int> > tab;
	st[v] = 1;

	for(int x : G[v]){
		if(x == p) continue;

		filhos++;
		bool aux = dfs(x,v);
		flag |= aux;
		st[v] += st[x];

		if(!aux){
			tab[st[x]].push_back(x);
		}
	}

	for(auto &it : tab){
		while((it.ss).size() > 1){
			G[v].erase((it.ss).back());
			st[v] -= st[(it.ss).back()];
			filhos--;
			// n -= it.ff;
			(it.ss).pop_back();
		}
	}

	return flag | (filhos >= 2);
}

void check(int v, int p){
	// printf(". %d %d: %d\n",p,v,st[v]);
	ans++;
	if(G[v].size() > 2) deuruim = 1;

	for(int x : G[v])
		if(x != p) check(x,v);
}

int main(){
	int a,b;

	scanf("%d", &n);

	for(int i = 1; i < n; i++){
		scanf("%d%d", &a, &b);
		G[a].insert(b);
		G[b].insert(a);
		dg[a]++; dg[b]++;
	}

	int root = find_center();

	dfs(root, root);

	ans = 0;
	check(root, root);

	if(deuruim) printf("-1\n");
	else{
		ans--;
		while(ans && ans%2 == 0) ans /= 2;
		printf("%d\n", ans);
	}

	return 0;
}


#include <bits/stdc++.h>

#define ff first
#define ss second
#define mp make_pair

using namespace std;

typedef long long ll;

set<int> G[200005];
int st[200005], n, ans;
bool deuruim;

int val[2], qual_ponta, diametro, ponta[2];
int dist[2][200005];

void dia(int v, int p, int d){
	diametro = max(diametro, d);

	if(d > val[qual_ponta]){
		val[qual_ponta] = d;
		ponta[qual_ponta] = v;
	}

	for(int u : G[v]){
		if(p != u) dia(u,v,d+1);
	}
}

void dia2(int v, int p, int d, int tp){
	dist[tp][v] = d;

	for(int u : G[v]){
		if(p != u) dia2(u,v,d+1,tp);
	}
}

void calc(int v, int p){
	st[v] = 1;
	for(int x : G[v]){
		if(x != p){
			calc(x, v);
			st[v] += st[x];
		}
	}
}

bool dfs(int v, int p){
	int filhos = 0;
	bool flag = 0;
	map<int, vector<int> > tab;
	st[v] = 1;

	for(int x : G[v]){
		if(x == p) continue;

		filhos++;
		bool aux = dfs(x,v);
		flag |= aux;
		st[v] += st[x];

		if(!aux){
			tab[st[x]].push_back(x);
		}
	}

	for(auto &it : tab){
		while((it.ss).size() > 1){
			G[v].erase((it.ss).back());
			st[v] -= st[(it.ss).back()];
			filhos--;
			// n -= it.ff;
			(it.ss).pop_back();
		}
	}

	return flag | (filhos >= 2);
}

void check(int v, int p){
	// printf(". %d %d: %d\n",p,v,st[v]);
	ans++;
	if(G[v].size() > 2) deuruim = 1;

	for(int x : G[v])
		if(x != p) check(x,v);
}

int main(){
	int a,b;

	scanf("%d", &n);

	for(int i = 1; i < n; i++){
		scanf("%d%d", &a, &b);
		G[a].insert(b);
		G[b].insert(a);
	}

	diametro = 0;

	val[0] = val[1] = -1;
	qual_ponta = 0;
	dia(1,0,0);
	qual_ponta = 1;
	dia(ponta[0],0,0);

	dia2(ponta[0],0,0,0);
	dia2(ponta[1],0,0,1);

	int root = -1;

	for(int i = 1; i <= n; i++){
		if((diametro+1)/2 >= dist[0][i] && (diametro+1)/2 >= dist[1][i])
			root = i;
	}

	dfs(root, root);

	ans = 0;
	check(root, root);

	if(deuruim) printf("-1\n");
	else{
		ans--;
		while(ans && ans%2 == 0) ans /= 2;
		printf("%d\n", ans);
	}

	return 0;
}


#include <bits/stdc++.h>

#define ff first
#define ss second
#define mp make_pair

using namespace std;

typedef long long ll;

set<int> G[200005];
int st[200005], n, ans;
bool deuruim;

void calc(int v, int p){
	st[v] = 1;
	for(int x : G[v]){
		if(x != p){
			calc(x, v);
			st[v] += st[x];
		}
	}
}

int find_centroid(int v, int p){
	for(int x : G[v]){
		if(x == p) continue;
		if(st[x]*2 > n) return find_centroid(x,v);
	}
	return v;
}

bool dfs(int v, int p){
	int filhos = 0;
	bool flag = 0;
	map<int, vector<int> > tab;

	for(int x : G[v]){
		if(x == p) continue;

		filhos++;
		bool aux = dfs(x,v);
		flag |= aux;

		if(!aux){
			tab[st[x]].push_back(x);
		}
	}

	for(auto &it : tab){
		while((it.ss).size() > 1){
			G[v].erase((it.ss).back());
			st[v] -= st[(it.ss).back()];
			filhos--;
			n -= st[(it.ss).back()];
			(it.ss).pop_back();
		}
	}

	return flag | (filhos >= 2);
}

void check(int v, int p){
	ans++;
	if(G[v].size() > 2) deuruim = 1;

	for(int x : G[v])
		if(x != p) check(x,v);
}

int main(){
	int a,b;

	scanf("%d", &n);

	for(int i = 1; i < n; i++){
		scanf("%d%d", &a, &b);
		G[a].insert(b);
		G[b].insert(a);
	}

	int root = 1;
	int last = n;
	while(1){
		calc(root,root);
		root = find_centroid(root,root);
		calc(root,root);

		dfs(root, root);

		if(last == n) break;
		last = n;
	}

	ans = 0;
	check(root, root);

	if(deuruim) printf("-1\n");
	else{
		ans--;
		while(ans && ans%2 == 0) ans /= 2;
		printf("%d\n", ans);
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define inf (int)1e6
int n;
vector<int> from[200005];
map<int,map<int,int> > mem;
int f(int x,int last)
{
    int i,now;
    if(!mem[x][last])
    {
        now = -1;
        for(i=0;i<from[x].size();i++)
        {
            if(from[x][i]==last) continue;
            if(now==-1) now = f(from[x][i],x);
            else if(now!=f(from[x][i],x)) now = inf;
        }
        mem[x][last] = min(inf,now+1);
    }
    return mem[x][last];
}
main()
{
    int i,x,y,ans;
    scanf("%d",&n);
    for(i=0;i<n-1;i++)
    {
        scanf("%d%d",&x,&y);
        from[x].push_back(y); from[y].push_back(x);
    }
    ans = inf;
    for(i=1;i<=n;i++) ans = min(ans,f(i,0));
    if(ans==inf) printf("-1");
    else printf("%d",ans);
}
/*
8
1 2
1 3
3 4
3 6
4 5
6 7
6 8
*/


#include<bits/stdc++.h>
using namespace std;
#define inf (int)1e7
int n;
vector<int> from[200005];
map<int,map<int,int> > mem;
int f(int x,int last)
{
    int i,now,t;
    if(!mem[x][last])
    {
        now = -1;
        for(i=0;i<from[x].size();i++)
        {
            if(from[x][i]==last) continue;
            t = f(from[x][i],x);
            if(now==-1) now = t;
            else if(now!=t) now = inf;
        }
        mem[x][last] = min(inf,now+1);
        //printf("mem %d %d = %d\n",x,last,mem[x][last]);
    }
    return mem[x][last];
}
main()
{
    int i,x,y,ans;
    scanf("%d",&n);
    for(i=0;i<n-1;i++)
    {
        scanf("%d%d",&x,&y);
        from[x].push_back(y); from[y].push_back(x);
    }
    ans = inf;
    for(i=1;i<=n;i++) ans = min(ans,f(i,0));
    if(ans>=n) printf("-1");
    else
    {
        while(ans>1 && ans%2==0) ans/=2;
        printf("%d",ans);
    }
}


#include<bits/stdc++.h>
using namespace std;
#define inf (int)1e7
int n,m;
int sz[200005];
int mem[500005];
vector<pair<int,int> > from[200005];
int f(int x,int last,int num)
{
    int i,a,b,t;
    if(last && sz[x]==1) return 0;
    if(!mem[num])
    {
        a = b = -1;
        for(i=0;i<sz[x];i++)
        {
            if(from[x][i].first==last) continue;
            t = f(from[x][i].first,x,from[x][i].second)+1;
            if(a==-1) a = t;
            else if(a!=t && b==-1) b = t;
            else if(a!=t && b!=t) a = b = inf;
        }
        if(a==-1 && b==-1) mem[num] = inf;
        else if(b==-1) mem[num] = min(inf,a);
        else if(!last) mem[num] = min(inf,a+b);
        else mem[num] = inf;
        //printf("mem %d %d = %d     [%d and %d]\n",x,last,mem[num],a,b);
    }
    return mem[num];
}
main()
{
    int i,x,y,ans;
    scanf("%d",&n);
    m = 1;
    for(i=0;i<n-1;i++)
    {
        scanf("%d%d",&x,&y);
        from[x].push_back({y,m++}); from[y].push_back({x,m++});
    }
    ans = inf;
    for(i=1;i<=n;i++) sz[i] = from[i].size();
    for(i=1;i<=n;i++) ans = min(ans,f(i,0,m++));
    if(ans>=n) printf("-1");
    else
    {
        while(ans>1 && ans%2==0) ans/=2;
        printf("%d",ans);
    }
}
/*
8
1 2
1 3
1 4
1 5
5 6
5 7
5 8
*/


#include<bits/stdc++.h>
using namespace std;
#define inf (int)1e6
int n;
vector<int> from[200005];
map<int,map<int,int> > mem;
int f(int x,int last)
{
    int i,now;
    if(!mem[x][last])
    {
        now = -1;
        for(i=0;i<from[x].size();i++)
        {
            if(from[x][i]==last) continue;
            if(now==-1) now = f(from[x][i],x);
            else if(now!=f(from[x][i],x)) now = inf;
        }
        mem[x][last] = min(inf,now+1);
    }
    return mem[x][last];
}
main()
{
    int i,x,y,ans;
    scanf("%d",&n);
    for(i=0;i<n-1;i++)
    {
        scanf("%d%d",&x,&y);
        from[x].push_back(y); from[y].push_back(x);
    }
    ans = inf;
    for(i=1;i<=n;i++) ans = min(ans,f(i,0));
    if(ans==inf) printf("-1");
    else
    {
        while(ans>1 && ans%2==0) ans/=2;
        printf("%d",ans);
    }
}
/*
8
1 2
1 3
3 4
3 6
4 5
6 7
6 8
*/


#include<bits/stdc++.h>
using namespace std;
#define inf (int)1e7
int n,m;
int sz[200005];
int mem[1000005];
vector<pair<int,int> > from[200005];
int f(int x,int last,int num)
{
    int i,a,b,t;
    if(last && sz[x]==1) return 0;
    if(!mem[num])
    {
        a = b = -1;
        for(i=0;i<sz[x];i++)
        {
            if(from[x][i].first==last) continue;
            t = f(from[x][i].first,x,from[x][i].second)+1;
            if(a==-1) a = t;
            else if(a!=t && b==-1) b = t;
            else if(a!=t && b!=t) a = b = inf;
        }
        if(a==-1 && b==-1) mem[num] = inf;
        else if(b==-1) mem[num] = min(inf,a);
        else if(!last) mem[num] = min(inf,a+b);
        else mem[num] = inf;
        //printf("mem %d %d = %d     [%d and %d]\n",x,last,mem[num],a,b);
    }
    return mem[num];
}
main()
{
    int i,x,y,ans;
    scanf("%d",&n);
    m = 1;
    for(i=0;i<n-1;i++)
    {
        scanf("%d%d",&x,&y);
        from[x].push_back({y,m++}); from[y].push_back({x,m++});
    }
    ans = inf;
    for(i=1;i<=n;i++) sz[i] = from[i].size();
    for(i=1;i<=n;i++) ans = min(ans,f(i,0,m++));
    if(ans>=n) printf("-1");
    else
    {
        while(ans>1 && ans%2==0) ans/=2;
        printf("%d",ans);
    }
}
/*
8
1 2
1 3
1 4
1 5
5 6
5 7
5 8
*/


#include<bits/stdc++.h>
using namespace std;
#define inf (int)1e7
int n;
int sz[200005];
vector<int> from[200005];
map<int,map<int,int> > mem;
int f(int x,int last)
{
    int i,a,b,t;
    if(last && sz[x]==1) return 0;
    if(!mem[x][last])
    {
        a = b = -1;
        for(i=0;i<from[x].size();i++)
        {
            if(from[x][i]==last) continue;
            t = f(from[x][i],x)+1;
            if(a==-1) a = t;
            else if(a!=t && b==-1) b = t;
            else if(a!=t && b!=t) a = b = inf;
        }
        if(a==-1 && b==-1) mem[x][last] = inf;
        else if(b==-1) mem[x][last] = min(inf,a);
        else if(!last) mem[x][last] = min(inf,a+b);
        else mem[x][last] = inf;
        //printf("mem %d %d = %d     [%d and %d]\n",x,last,mem[x][last],a,b);
    }
    return mem[x][last];
}
main()
{
    int i,x,y,ans;
    scanf("%d",&n);
    for(i=0;i<n-1;i++)
    {
        scanf("%d%d",&x,&y);
        from[x].push_back(y); from[y].push_back(x);
    }
    ans = inf;
    for(i=1;i<=n;i++) sz[i] = from[i].size();
    for(i=1;i<=n;i++) ans = min(ans,f(i,0));
    if(ans>=n) printf("-1");
    else
    {
        while(ans>1 && ans%2==0) ans/=2;
        printf("%d",ans);
    }
}
/*
8
1 2
1 3
1 4
1 5
5 6
5 7
5 8
*/


#include<bits/stdc++.h>
using namespace std;
#define inf (int)1e7
int n;
int sz[200005];
vector<int> from[200005];
unordered_map<int,unordered_map<int,int> > mem;
int f(int x,int last)
{
    int i,a,b,t;
    if(last && sz[x]==1) return 0;
    if(!mem[x][last])
    {
        a = b = -1;
        for(i=0;i<from[x].size();i++)
        {
            if(from[x][i]==last) continue;
            t = f(from[x][i],x)+1;
            if(a==-1) a = t;
            else if(a!=t && b==-1) b = t;
            else if(a!=t && b!=t) a = b = inf;
        }
        if(a==-1 && b==-1) mem[x][last] = inf;
        else if(b==-1) mem[x][last] = min(inf,a);
        else if(!last) mem[x][last] = min(inf,a+b);
        else mem[x][last] = inf;
        //printf("mem %d %d = %d     [%d and %d]\n",x,last,mem[x][last],a,b);
    }
    return mem[x][last];
}
main()
{
    int i,x,y,ans;
    scanf("%d",&n);
    for(i=0;i<n-1;i++)
    {
        scanf("%d%d",&x,&y);
        from[x].push_back(y); from[y].push_back(x);
    }
    ans = inf;
    for(i=1;i<=n;i++) sz[i] = from[i].size();
    for(i=1;i<=n;i++) ans = min(ans,f(i,0));
    if(ans>=n) printf("-1");
    else
    {
        while(ans>1 && ans%2==0) ans/=2;
        printf("%d",ans);
    }
}
/*
8
1 2
1 3
1 4
1 5
5 6
5 7
5 8
*/


#include<bits/stdc++.h>
using namespace std;
#define inf (int)1e6
int n,m;
int d[200005];
int sz[200005];
int mem[1000005];
vector<pair<int,int> > from[200005];
int f(int x,int last,int num)
{
    int i,a,b,t;
    if(last && sz[x]==1) return 0;
    if(!mem[num])
    {
        a = b = -1;
        for(i=0;i<sz[x];i++)
        {
            if(from[x][i].first==last) continue;
            t = f(from[x][i].first,x,from[x][i].second)+1;
            if(a==-1) a = t;
            else if(a!=t && b==-1) b = t;
            else if(a!=t && b!=t) a = b = inf;
        }
        if(a==-1 && b==-1) mem[num] = inf;
        else if(b==-1) mem[num] = a;
        else if(!last) mem[num] = a+b;
        else mem[num] = inf;
        if(mem[num]>inf) mem[num] = inf;
        //printf("mem %d %d = %d     [%d and %d]\n",x,last,mem[num],a,b);
    }
    return mem[num];
}
void dfs(int x,int last)
{
    int i;
    if(d[x]) return ;
    d[x] = d[last]+1;
    for(i=0;i<from[x].size();i++) if(last!=from[x][i].first) dfs(from[x][i].first,x);
}
main()
{
    int i,x,y,ans;
    scanf("%d",&n);
    m = 1;
    for(i=0;i<n-1;i++)
    {
        scanf("%d%d",&x,&y);
        from[x].push_back({y,m++}); from[y].push_back({x,m++});
    }
    ans = inf;
    for(i=1;i<=n;i++) sz[i] = from[i].size();
    dfs(1,0);
    x = 1;
    for(i=1;i<=n;i++) if(d[i]>d[x]) x = i;
    memset(d,0,sizeof(d));
    dfs(x,0);
    for(i=1;i<=n;i++) if(d[i]>d[x]) x = i;
    for(i=1;i<=n;i++) if(d[i]==d[x]/2 || d[i]==d[x]/2+1 || d[i]==d[x]/2-1) ans = min(ans,f(i,0,m++));
    //for(i=1;i<=n;i++) ans = min(ans,f(i,0,m++));
    if(ans>=n) printf("-1");
    else
    {
        while(ans>1 && ans%2==0) ans/=2;
        printf("%d",ans);
    }
}
/*
8
1 2
1 3
1 4
1 5
5 6
5 7
5 8
*/


#include<bits/stdc++.h>
using namespace std;
#define inf (int)1e7
int n;
vector<int> from[200005];
map<int,map<int,int> > mem;
int f(int x,int last)
{
    int i,a,b,t;
    if(last && from[x].size()==1) return 0;
    if(!mem[x][last])
    {
        a = b = -1;
        for(i=0;i<from[x].size();i++)
        {
            if(from[x][i]==last) continue;
            t = f(from[x][i],x)+1;
            if(a==-1) a = t;
            else if(a!=t && b==-1) b = t;
            else if(a!=t && b!=t) a = b = inf;
        }
        if(a==-1 && b==-1) mem[x][last] = inf;
        else if(b==-1) mem[x][last] = min(inf,a);
        else if(!last) mem[x][last] = min(inf,a+b);
        else mem[x][last] = inf;
        //printf("mem %d %d = %d     [%d and %d]\n",x,last,mem[x][last],a,b);
    }
    return mem[x][last];
}
main()
{
    int i,x,y,ans;
    scanf("%d",&n);
    for(i=0;i<n-1;i++)
    {
        scanf("%d%d",&x,&y);
        from[x].push_back(y); from[y].push_back(x);
    }
    ans = inf;
    for(i=1;i<=n;i++) ans = min(ans,f(i,0));
    if(ans>=n) printf("-1");
    else
    {
        while(ans>1 && ans%2==0) ans/=2;
        printf("%d",ans);
    }
}
/*
8
1 2
1 3
1 4
1 5
5 6
5 7
5 8
*/


#include<bits/stdc++.h>
using namespace std;
#define inf (int)1e7
int n;
vector<int> from[200005];
unordered_map<int,unordered_map<int,int> > mem;
int f(int x,int last)
{
    int i,a,b,t;
    if(last && from[x].size()==1) return 0;
    if(!mem[x][last])
    {
        a = b = -1;
        for(i=0;i<from[x].size();i++)
        {
            if(from[x][i]==last) continue;
            t = f(from[x][i],x)+1;
            if(a==-1) a = t;
            else if(a!=t && b==-1) b = t;
            else if(a!=t && b!=t) a = b = inf;
        }
        if(a==-1 && b==-1) mem[x][last] = inf;
        else if(b==-1) mem[x][last] = min(inf,a);
        else if(!last) mem[x][last] = min(inf,a+b);
        else mem[x][last] = inf;
        //printf("mem %d %d = %d     [%d and %d]\n",x,last,mem[x][last],a,b);
    }
    return mem[x][last];
}
main()
{
    int i,x,y,ans;
    scanf("%d",&n);
    for(i=0;i<n-1;i++)
    {
        scanf("%d%d",&x,&y);
        from[x].push_back(y); from[y].push_back(x);
    }
    ans = inf;
    for(i=1;i<=n;i++) ans = min(ans,f(i,0));
    if(ans>=n) printf("-1");
    else
    {
        while(ans>1 && ans%2==0) ans/=2;
        printf("%d",ans);
    }
}
/*
8
1 2
1 3
1 4
1 5
5 6
5 7
5 8
*/


#include<bits/stdc++.h>
using namespace std;
#define inf (int)1e6
int n,m;
int sz[200005];
int mem[1000005];
vector<pair<int,int> > from[200005];
int f(int x,int last,int num)
{
    int i,a,b,t;
    if(last && sz[x]==1) return 0;
    if(!mem[num])
    {
        a = b = -1;
        for(i=0;i<sz[x];i++)
        {
            if(from[x][i].first==last) continue;
            t = f(from[x][i].first,x,from[x][i].second)+1;
            if(a==-1) a = t;
            else if(a!=t && b==-1) b = t;
            else if(a!=t && b!=t) a = b = inf;
        }
        if(a==-1 && b==-1) mem[num] = inf;
        else if(b==-1) mem[num] = a;
        else if(!last) mem[num] = a+b;
        else mem[num] = inf;
        if(mem[num]>inf) mem[num] = inf;
        //printf("mem %d %d = %d     [%d and %d]\n",x,last,mem[num],a,b);
    }
    return mem[num];
}
main()
{
    int i,x,y,ans;
    scanf("%d",&n);
    m = 1;
    for(i=0;i<n-1;i++)
    {
        scanf("%d%d",&x,&y);
        from[x].push_back({y,m++}); from[y].push_back({x,m++});
    }
    ans = inf;
    for(i=1;i<=n;i++) sz[i] = from[i].size();
    for(i=1;i<=n;i++) ans = min(ans,f(i,0,m++));
    if(ans>=n) printf("-1");
    else
    {
        while(ans>1 && ans%2==0) ans/=2;
        printf("%d",ans);
    }
}
/*
8
1 2
1 3
1 4
1 5
5 6
5 7
5 8
*/


/*
* @Author: wjh
* @Date:   2017-02-14 16:03:47
* @Last Modified by:   wjh
* @Last Modified time: 2017-02-14 18:56:35
*/

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>

#ifdef DEBUG
    const int MAXN = 200;
#else
    const int MAXN = 200010;
#endif

using namespace std;
const int INF = 0x3f3f3f3f;

int n;
vector<int> g[MAXN];

inline void insert(int u, int v) {
    g[u].push_back(v);
}

int ans = INF;

int fa[MAXN], sl[MAXN];
inline void dfs(int now) {
    for (auto to : g[now]) {
        if (to == fa[now]) continue;
        fa[to] = now;
        dfs(to);
        if (!sl[now]) sl[now] = sl[to] + 1;
        if (sl[to] + 1 != sl[now]) sl[now] = -1;
    }
    if (sl[now] == 0) sl[now] = 1;
}

inline void find(int now, int fal) {
    int n1 = 0, l1 = fal, l2 = 0;
    for (auto to : g[now]) {
        if (to == fa[now]) continue;
        if (sl[to] == -1) {
            if (n1) return; else n1 = to;
        } else if (!l1) l1 = sl[to];
        else if (l1 != sl[to]) {
            if (!l2 || l2 == sl[to]) l2 = sl[to]; else return;
        }
    }
    if (!fal) fal = l1;
    if (n1 && !l2 && fal == l1) {
        find(n1, l1 + 1);
    } else if (!l2) {
        ans = min(ans, l1);
        for (auto to : g[now]) {
            if (to == fa[now]) continue;
            find(to, l1 + 1);
        }
    } else {
        ans = min(ans, l1 + l2);
        int p1 = 0, p2 = 0;
        for (auto to : g[now]) {
            if (to == fa[now]) continue;
            if (sl[to] == l2) p2 = p2 ? -1 : to;
        }
        if (p2 != -1) find(p2, l1 + 1);
    }
}

int main() {

    scanf("%d", &n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        insert(u, v), insert(v, u);
    }
    dfs(1);
    find(1, 0);
    printf("%d\n", ans == INF ? -1 : ans);

    return 0;
}


/*
* @Author: wjh
* @Date:   2017-02-14 16:03:47
* @Last Modified by:   wjh
* @Last Modified time: 2017-02-14 19:40:20
*/

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>

#ifdef DEBUG
    const int MAXN = 200;
#else
    const int MAXN = 200010;
#endif

using namespace std;
const int INF = 0x3f3f3f3f;

int n;
vector<int> g[MAXN];

inline void insert(int u, int v) {
    g[u].push_back(v);
}

int ans = INF;

int fa[MAXN], sl[MAXN];
inline void dfs(int now) {
    for (auto to : g[now]) {
        if (to == fa[now]) continue;
        fa[to] = now;
        dfs(to);
        if (!sl[now]) sl[now] = sl[to] + 1;
        if (sl[to] + 1 != sl[now]) sl[now] = -1;
        if (sl[to] == -1) sl[now] = -1;
    }
    if (sl[now] == 0) sl[now] = 1;
}

inline void find(int now, int fal) {
    int n1 = 0, l1 = fal, l2 = 0;
    for (auto to : g[now]) {
        if (to == fa[now]) continue;
        if (sl[to] == -1) {
            if (n1) return; else n1 = to;
        } else if (!l1) l1 = sl[to];
        else if (l1 != sl[to]) {
            if (!l2 || l2 == sl[to]) l2 = sl[to]; else return;
        }
    }

    if (!fal) fal = l1;
    if (n1 && !l2 && fal == l1) {
        find(n1, l1 + 1);
    } else if (!l2) {
        ans = min(ans, l1);
        for (auto to : g[now]) {
            if (to == fa[now]) continue;
            find(to, fa[now] ? l1 + 1 : 1);
        }
    } else {
        ans = min(ans, l1 + l2);
        int p1 = 0, p2 = 0;
        for (auto to : g[now]) {
            if (to == fa[now]) continue;
            if (sl[to] == l2) p2 = p2 ? -1 : to;
        }
        if (p2 != -1) find(p2, l1 + 1);
    }
}

int main() {

    scanf("%d", &n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        insert(u, v), insert(v, u);
    }
    dfs(1);
    find(1, 0);
    while (!(ans & 1)) ans >>= 1;
    printf("%d\n", ans == INF ? -1 : ans);

    return 0;
}


/*
* @Author: wjh
* @Date:   2017-02-14 16:03:47
* @Last Modified by:   wjh
* @Last Modified time: 2017-02-14 19:40:20
*/

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>

#ifdef DEBUG
    const int MAXN = 200;
#else
    const int MAXN = 200010;
#endif

using namespace std;
const int INF = 0x3f3f3f3f;

int n;
vector<int> g[MAXN];

inline void insert(int u, int v) {
    g[u].push_back(v);
}

int ans = INF;

int fa[MAXN], sl[MAXN];
inline void dfs(int now) {
    for (auto to : g[now]) {
        if (to == fa[now]) continue;
        fa[to] = now;
        dfs(to);
        if (!sl[now]) sl[now] = sl[to] + 1;
        if (sl[to] + 1 != sl[now]) sl[now] = -1;
        if (sl[to] == -1) sl[now] = -1;
    }
    if (sl[now] == 0) sl[now] = 1;
}

inline void find(int now, int fal) {
    int n1 = 0, l1 = fal, l2 = 0;
    for (auto to : g[now]) {
        if (to == fa[now]) continue;
        if (sl[to] == -1) {
            if (n1) return; else n1 = to;
        } else if (!l1) l1 = sl[to];
        else if (l1 != sl[to]) {
            if (!l2 || l2 == sl[to]) l2 = sl[to]; else return;
        }
    }

    if (!fal) fal = l1;
    if (n1 && !l2 && fal == l1) {
        find(n1, fa[now] ? l1 + 1 : 1);
    } else if (!l2) {
        ans = min(ans, l1);
        for (auto to : g[now]) {
            if (to == fa[now]) continue;
            find(to, fa[now] ? l1 + 1 : 1);
        }
    } else {
        ans = min(ans, l1 + l2);
        int p1 = 0, p2 = 0;
        for (auto to : g[now]) {
            if (to == fa[now]) continue;
            if (sl[to] == l2) p2 = p2 ? -1 : to;
        }
        if (p2 != -1) find(p2, l1 + 1);
    }
}

int main() {

    scanf("%d", &n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        insert(u, v), insert(v, u);
    }
    dfs(1);
    find(1, 0);
    printf("%d\n", ans == INF ? -1 : ans);

    return 0;
}


/*
* @Author: wjh
* @Date:   2017-02-14 16:03:47
* @Last Modified by:   wjh
* @Last Modified time: 2017-02-14 19:40:20
*/

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>

#ifdef DEBUG
    const int MAXN = 200;
#else
    const int MAXN = 200010;
#endif

using namespace std;
const int INF = 0x3f3f3f3f;

int n;
vector<int> g[MAXN];

inline void insert(int u, int v) {
    g[u].push_back(v);
}

int ans = INF;

int fa[MAXN], sl[MAXN];
inline void dfs(int now) {
    for (auto to : g[now]) {
        if (to == fa[now]) continue;
        fa[to] = now;
        dfs(to);
        if (!sl[now]) sl[now] = sl[to] + 1;
        if (sl[to] + 1 != sl[now]) sl[now] = -1;
        if (sl[to] == -1) sl[now] = -1;
    }
    if (sl[now] == 0) sl[now] = 1;
}

inline void find(int now, int fal) {
    int n1 = 0, l1 = fal, l2 = 0;
    for (auto to : g[now]) {
        if (to == fa[now]) continue;
        if (sl[to] == -1) {
            if (n1) return; else n1 = to;
        } else if (!l1) l1 = sl[to];
        else if (l1 != sl[to]) {
            if (!l2 || l2 == sl[to]) l2 = sl[to]; else return;
        }
    }

    if (!fal) fal = l1;
    if (n1) {
        if (!l2 && fal == l1) find(n1, l1 + 1);
    } else if (!l2) {
        ans = min(ans, l1);
        for (auto to : g[now]) {
            if (to == fa[now]) continue;
            find(to, fa[now] ? l1 + 1 : 1);
        }
    } else {
        ans = min(ans, l1 + l2);
        int p1 = 0, p2 = 0;
        for (auto to : g[now]) {
            if (to == fa[now]) continue;
            if (sl[to] == l2) p2 = p2 ? -1 : to;
        }
        if (p2 != -1) find(p2, l1 + 1);
    }
}

int main() {

    scanf("%d", &n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        insert(u, v), insert(v, u);
    }
    dfs(1);
    find(1, 0);
    while (!(ans & 1)) ans >>= 1;
    printf("%d\n", ans == INF ? -1 : ans);

    return 0;
}


/*
* @Author: wjh
* @Date:   2017-02-14 16:03:47
* @Last Modified by:   wjh
* @Last Modified time: 2017-02-14 18:41:02
*/

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>

#ifdef DEBUG
    const int MAXN = 200;
#else
    const int MAXN = 200010;
#endif

using namespace std;
const int INF = 0x3f3f3f3f;

int n;
vector<int> g[MAXN];

inline void insert(int u, int v) {
    g[u].push_back(v);
}

int ans = INF;

int fa[MAXN], sl[MAXN];
inline void dfs(int now) {
    for (auto to : g[now]) {
        if (to == fa[now]) continue;
        fa[to] = now;
        dfs(to);
        if (!sl[now]) sl[now] = sl[to] + 1;
        if (sl[to] + 1 != sl[now]) sl[now] = -1;
    }
    if (sl[now] == 0) sl[now] = 1;
}

inline void find(int now, int fal) {
    int n1 = 0, l1 = fal, l2 = 0;
    for (auto to : g[now]) {
        if (to == fa[now]) continue;
        if (sl[to] == -1) {
            if (n1) return; else n1 = to;
        } else if (!l1) l1 = sl[to];
        else if (l1 != sl[to]) {
            if (!l2 || l2 == sl[to]) l2 = sl[to]; else return;
        }
    }
    if (!fal) fal = l1;
    if (n1 && !l2 && fal == l1) {
        find(n1, l1 + 1);
    } else if (!l2) {
        ans = min(ans, l1);
    } else {
        ans = min(ans, l1 + l2);
    }
}

int main() {

    scanf("%d", &n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        insert(u, v), insert(v, u);
    }
    dfs(1);
    find(1, 0);
    printf("%d\n", ans == INF ? -1 : ans);

    return 0;
}


/*
* @Author: wjh
* @Date:   2017-02-14 16:03:47
* @Last Modified by:   wjh
* @Last Modified time: 2017-02-14 19:40:20
*/

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>

#ifdef DEBUG
    const int MAXN = 200;
#else
    const int MAXN = 200010;
#endif

using namespace std;
const int INF = 0x3f3f3f3f;

int n;
vector<int> g[MAXN];

inline void insert(int u, int v) {
    g[u].push_back(v);
}

int ans = INF;

int fa[MAXN], sl[MAXN];
inline void dfs(int now) {
    for (auto to : g[now]) {
        if (to == fa[now]) continue;
        fa[to] = now;
        dfs(to);
        if (!sl[now]) sl[now] = sl[to] + 1;
        if (sl[to] + 1 != sl[now]) sl[now] = -1;
    }
    if (sl[now] == 0) sl[now] = 1;
}

inline void find(int now, int fal) {
    int n1 = 0, l1 = fal, l2 = 0;
    for (auto to : g[now]) {
        if (to == fa[now]) continue;
        if (sl[to] == -1) {
            if (n1) return; else n1 = to;
        } else if (!l1) l1 = sl[to];
        else if (l1 != sl[to]) {
            if (!l2 || l2 == sl[to]) l2 = sl[to]; else return;
        }
    }

    if (!fal) fal = l1;
    if (n1 && !l2 && fal == l1) {
        find(n1, fa[now] ? l1 + 1 : 1);
    } else if (!l2) {
        ans = min(ans, l1);
        for (auto to : g[now]) {
            if (to == fa[now]) continue;
            find(to, fa[now] ? l1 + 1 : 1);
        }
    } else {
        ans = min(ans, l1 + l2);
        int p1 = 0, p2 = 0;
        for (auto to : g[now]) {
            if (to == fa[now]) continue;
            if (sl[to] == l2) p2 = p2 ? -1 : to;
        }
        if (p2 != -1) find(p2, l1 + 1);
    }
}

int main() {

    scanf("%d", &n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        insert(u, v), insert(v, u);
    }
    dfs(1);
    find(1, 0);
    printf("%d\n", ans == INF ? -1 : ans);

    return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define y0 vjfuyg
#define y1 jfskav
#define pb push_back
#define lson (k<<1)
#define rson (k<<1|1)
#define lowbit(x) ((x)&-(x))
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<" = "<<(x)<<endl
#define rep(i,s,t) for(int i=(s),_t=(t);i<_t;++i)
#define per(i,s,t) for(int i=(t)-1,_s=(s);i>=_s;--i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> veci;
const int inf=0x7fffffff,infty=(int)1e9,mod=(int)1e9+7,dxy[]={-1,0,1,0,-1};
const ll INF=1ll<<60;
const db pi=acos(-1),eps=1e-8;
template<class T>void rd(T &x)
{
	x=0;
	char c;
	while(c=getchar(),c<48);
	do x=x*10+(c^48);
		while(c=getchar(),c>47);
}
template<class T>void pt(T x)
{
	if(!x)
	{
		putchar('0');
		return;
	}
	static int stk[65];
	int tp=0;
	for(;x;x/=10)stk[tp++]=x%10;
	per(i,0,tp)putchar(stk[i]^48);
}
template<class T>inline void pts(T x)
{
	pt(x),putchar(' ');
}
template<class T>inline void ptn(T x)
{
	pt(x),putchar('\n');
}
template<class T>inline void Max(T &a,T b)
{
	if(b>a)a=b;
}
template<class T>inline void Min(T &a,T b)
{
	if(b<a)a=b;
}

const int N=(int)2e5+5;

int par[N],ID,mx_dist;
veci T[N];
bool ban[N];

void dfs(int u,int pre,int dist){
    if(dist>=mx_dist){
        ID=u;
        mx_dist=dist;
    }
    par[u]=pre;
    for(int v:T[u]){
        if(v==pre)continue;
        dfs(v,u,dist+1);
    }
}

bool mark[N];

veci calc(int u,int pre=0)
{
	veci arr;
	for(int v:T[u])
	{
		if(ban[v]||v==pre)continue;
		veci tmp=calc(v,u);
		if(tmp[0]==-1||siz(tmp)>1)return {-1};
		for(int elm:tmp)
		{
			arr.pb(elm+1);
			mark[elm+1]=true;
		}
	}
	veci res;
	for(int elm:arr)if(mark[elm])
	{
		mark[elm]=false;
		res.pb(elm);
	}
	if(res.empty())return {0};
	if(siz(res)>2)return {-1};
	return res;
}

int ans;
veci rem[N];

bool check1()
{
	rep(i,0,mx_dist+1)for(int elm:rem[i])
	{
//		debug(elm);//
		if(~elm&&elm&&elm!=i&&elm!=mx_dist-i)return false;
	}
	ans=mx_dist;
	return true;
}

bool check2()
{
	if(mx_dist&1)return false;
	if(siz(rem[mx_dist>>1])>1)return false;
	rep(i,0,mx_dist+1)
	{
		if((i<<1)==mx_dist)continue;
		for(int elm:rem[i])
		{
			if(~elm&&elm&&elm!=i&&elm!=mx_dist-i)return false;
		}
	}
	ans=rem[mx_dist>>1][0]+(mx_dist>>1);
	return true;
}

int main()
{
	int n;
	rd(n);
	for(int i=1,u,v;i<n;++i)
	{
		rd(u),rd(v);
		T[u].pb(v);
		T[v].pb(u);
	}
	mx_dist=0;
    dfs(1,0,0);
    dfs(ID,0,0);
    for(int u=ID;u;u=par[u])ban[u]=true;
	for(int u=ID,i=0;u;u=par[u],++i)rem[i]=calc(u);
	if(!check1()&&!check2())return puts("-1"),0;
//	debug(ans);//
	while(~ans&1)ans>>=1;
	ptn(ans);
	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define y0 vjfuyg
#define y1 jfskav
#define pb push_back
#define lson (k<<1)
#define rson (k<<1|1)
#define lowbit(x) ((x)&-(x))
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<" = "<<(x)<<endl
#define rep(i,s,t) for(int i=(s),_t=(t);i<_t;++i)
#define per(i,s,t) for(int i=(t)-1,_s=(s);i>=_s;--i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> veci;
const int inf=0x7fffffff,infty=(int)1e9,mod=(int)1e9+7,dxy[]={-1,0,1,0,-1};
const ll INF=1ll<<60;
const db pi=acos(-1),eps=1e-8;
template<class T>void rd(T &x)
{
	x=0;
	char c;
	while(c=getchar(),c<48);
	do x=x*10+(c^48);
		while(c=getchar(),c>47);
}
template<class T>void pt(T x)
{
	if(!x)
	{
		putchar('0');
		return;
	}
	static int stk[65];
	int tp=0;
	for(;x;x/=10)stk[tp++]=x%10;
	per(i,0,tp)putchar(stk[i]^48);
}
template<class T>inline void pts(T x)
{
	pt(x),putchar(' ');
}
template<class T>inline void ptn(T x)
{
	pt(x),putchar('\n');
}
template<class T>inline void Max(T &a,T b)
{
	if(b>a)a=b;
}
template<class T>inline void Min(T &a,T b)
{
	if(b<a)a=b;
}

const int N=(int)2e5+5;

int par[N],ID,mx_dist;
veci T[N];
bool ban[N];

void dfs(int u,int pre,int dist){
    if(dist>=mx_dist){
        ID=u;
        mx_dist=dist;
    }
    par[u]=pre;
    for(int v:T[u]){
        if(v==pre)continue;
        dfs(v,u,dist+1);
    }
}

bool mark[N];

veci calc(int u,int pre=0)
{
	veci arr;
	for(int v:T[u])
	{
		if(ban[v]||v==pre)continue;
		veci tmp=calc(v,u);
		if(tmp[0]==-1||siz(tmp)>1)return {-1};
		for(int elm:tmp)
		{
			arr.pb(elm+1);
			mark[elm+1]=true;
		}
	}
	veci res;
	for(int elm:arr)if(mark[elm])
	{
		mark[elm]=false;
		res.pb(elm);
	}
	if(res.empty())return {0};
	if(siz(res)>2)return {-1};
	return res;
}

int ans;
veci rem[N];

bool check1()
{
	rep(i,0,mx_dist+1)for(int elm:rem[i])
	{
//		debug(elm);//
		if(~elm&&elm&&elm!=i&&elm!=mx_dist-i)return false;
	}
	ans=mx_dist;
	return true;
}

bool check2()
{
	if(mx_dist&1)return false;
	rep(i,0,mx_dist+1)
	{
		if((i<<1)==mx_dist)continue;
		for(int elm:rem[i])
		{
			if(~elm&&elm&&elm!=i&&elm!=mx_dist-i)return false;
		}
	}
	int cnt=0;
	for(int elm:rem[mx_dist>>1])
	{
		if(!(~elm&&elm&&elm!=(mx_dist>>1)))continue;
		++cnt;
	}
	if(cnt>1)return false;
	ans=rem[mx_dist>>1][0]+(mx_dist>>1);
	return true;
}

int main()
{
	int n;
	rd(n);
	for(int i=1,u,v;i<n;++i)
	{
		rd(u),rd(v);
		T[u].pb(v);
		T[v].pb(u);
	}
	mx_dist=0;
    dfs(1,0,0);
    dfs(ID,0,0);
    for(int u=ID;u;u=par[u])ban[u]=true;
	for(int u=ID,i=0;u;u=par[u],++i)rem[i]=calc(u);
	if(!check1()&&!check2())return puts("-1"),0;
//	debug(ans);//
	while(~ans&1)ans>>=1;
	ptn(ans);
	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define y0 vjfuyg
#define y1 jfskav
#define pb push_back
#define lson (k<<1)
#define rson (k<<1|1)
#define lowbit(x) ((x)&-(x))
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<" = "<<(x)<<endl
#define rep(i,s,t) for(int i=(s),_t=(t);i<_t;++i)
#define per(i,s,t) for(int i=(t)-1,_s=(s);i>=_s;--i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> veci;
const int inf=0x7fffffff,infty=(int)1e9,mod=(int)1e9+7,dxy[]={-1,0,1,0,-1};
const ll INF=1ll<<60;
const db pi=acos(-1),eps=1e-8;
template<class T>void rd(T &x)
{
	x=0;
	char c;
	while(c=getchar(),c<48);
	do x=x*10+(c^48);
		while(c=getchar(),c>47);
}
template<class T>void pt(T x)
{
	if(!x)
	{
		putchar('0');
		return;
	}
	static int stk[65];
	int tp=0;
	for(;x;x/=10)stk[tp++]=x%10;
	per(i,0,tp)putchar(stk[i]^48);
}
template<class T>inline void pts(T x)
{
	pt(x),putchar(' ');
}
template<class T>inline void ptn(T x)
{
	pt(x),putchar('\n');
}
template<class T>inline void Max(T &a,T b)
{
	if(b>a)a=b;
}
template<class T>inline void Min(T &a,T b)
{
	if(b<a)a=b;
}

const int N=(int)2e5+5;

int par[N],ID,mx_dist;
veci T[N];
bool ban[N];

void dfs(int u,int pre,int dist){
    if(dist>=mx_dist){
        ID=u;
        mx_dist=dist;
    }
    par[u]=pre;
    for(int v:T[u]){
        if(v==pre)continue;
        dfs(v,u,dist+1);
    }
}

bool mark[N];

veci calc(int u,int pre=0)
{
	veci arr;
	for(int v:T[u])
	{
		if(ban[v]||v==pre)continue;
		veci tmp=calc(v,u);
		if(tmp[0]==-1)return {-1};
		for(int elm:tmp)
		{
			arr.pb(elm+1);
			mark[elm+1]^=1;
		}
	}
	veci res;
	for(int elm:arr)if(mark[elm])
	{
		mark[elm]=false;
		res.pb(elm);
	}
	if(res.empty())return {0};
	if(siz(res)>2)return {-1};
	return res;
}

int ans;
veci rem[N];

bool check1()
{
	rep(i,0,mx_dist+1)for(int elm:rem[i])if(~elm&&elm&&elm!=i&&elm!=mx_dist-i)return false;
	ans=mx_dist;
	return true;
}

bool check2()
{
	if(mx_dist&1)return false;
	if(siz(rem[mx_dist>>1])>1)return false;
	rep(i,0,mx_dist+1)
	{
		if((i<<1)==mx_dist)continue;
		for(int elm:rem[i])
		{
			if(~elm&&elm&&elm!=i&&elm!=mx_dist-i)return false;
		}
	}
	ans=rem[mx_dist>>1][0]+(mx_dist>>1);
	return true;
}

int main()
{
	int n;
	rd(n);
	for(int i=1,u,v;i<n;++i)
	{
		rd(u),rd(v);
		T[u].pb(v);
		T[v].pb(u);
	}
	mx_dist=0;
    dfs(1,0,0);
    dfs(ID,0,0);
    for(int u=ID;u;u=par[u])ban[u]=true;
	for(int u=ID,i=0;u;u=par[u],++i)rem[i]=calc(u);
	if(!check1()&&!check2())return puts("-1"),0;
	while(~ans&1)ans>>=1;
	ptn(ans);
	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define y0 vjfuyg
#define y1 jfskav
#define pb push_back
#define lson (k<<1)
#define rson (k<<1|1)
#define lowbit(x) ((x)&-(x))
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<" = "<<(x)<<endl
#define rep(i,s,t) for(int i=(s),_t=(t);i<_t;++i)
#define per(i,s,t) for(int i=(t)-1,_s=(s);i>=_s;--i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> veci;
const int inf=0x7fffffff,infty=(int)1e9,mod=(int)1e9+7,dxy[]={-1,0,1,0,-1};
const ll INF=1ll<<60;
const db pi=acos(-1),eps=1e-8;
template<class T>void rd(T &x)
{
	x=0;
	char c;
	while(c=getchar(),c<48);
	do x=x*10+(c^48);
		while(c=getchar(),c>47);
}
template<class T>void pt(T x)
{
	if(!x)
	{
		putchar('0');
		return;
	}
	static int stk[65];
	int tp=0;
	for(;x;x/=10)stk[tp++]=x%10;
	per(i,0,tp)putchar(stk[i]^48);
}
template<class T>inline void pts(T x)
{
	pt(x),putchar(' ');
}
template<class T>inline void ptn(T x)
{
	pt(x),putchar('\n');
}
template<class T>inline void Max(T &a,T b)
{
	if(b>a)a=b;
}
template<class T>inline void Min(T &a,T b)
{
	if(b<a)a=b;
}

const int N=(int)2e5+5;

int par[N],ID,mx_dist;
veci T[N];
bool ban[N];

void dfs(int u,int pre,int dist){
    if(dist>=mx_dist){
        ID=u;
        mx_dist=dist;
    }
    par[u]=pre;
    for(int v:T[u]){
        if(v==pre)continue;
        dfs(v,u,dist+1);
    }
}

veci calc(int u,int pre=0)
{
	veci res;
	for(int v:T[u])
	{
		if(ban[v]||v==pre)continue;
		veci tmp=calc(v,u);
		if(tmp[0]==-1||siz(tmp)>1)return {-1};
		for(int elm:tmp)res.pb(elm+1);
	}
	if(res.empty())return {0};
	sort(all(res));
	res.erase(unique(all(res)),res.end());
	if(siz(res)>2)return {-1};
	return res;
}

int ans;
veci rem[N];

bool check1()
{
	rep(i,0,mx_dist+1)for(int elm:rem[i])
	{
//		debug(elm);//
		if(elm&&elm!=i&&elm!=mx_dist-i)return false;
	}
	ans=mx_dist;
	return true;
}

bool check2()
{
	if(mx_dist&1)return false;
	rep(i,0,mx_dist+1)
	{
		if((i<<1)==mx_dist)continue;
		for(int elm:rem[i])
		{
			if(elm&&elm!=i&&elm!=mx_dist-i)return false;
		}
	}
	int cnt=0;
	for(int elm:rem[mx_dist>>1])
	{
		if(!(elm&&elm!=(mx_dist>>1)))continue;
		++cnt;
	}
	if(cnt>1)return false;
	ans=rem[mx_dist>>1][0]+(mx_dist>>1);
	return true;
}

int main()
{
//	freopen("data.in","r",stdin);//
//	freopen("pro.out","w",stdout);//
	int n;
	rd(n);
	for(int i=1,u,v;i<n;++i)
	{
		rd(u),rd(v);
		T[u].pb(v);
		T[v].pb(u);
	}
	mx_dist=0;
    dfs(1,0,0);
    dfs(ID,0,0);
    for(int u=ID;u;u=par[u])ban[u]=true;
	for(int u=ID,i=0;u;u=par[u],++i)
	{
		rem[i]=calc(u);
		if(rem[i][0]==-1)return puts("-1"),0;
//		debug(i),debug(u);//
//		for(int elm:rem[i])debug(elm);//
//		putchar('\n');//
	}
	if(!check1()&&!check2())return puts("-1"),0;
//	debug(ans);//
	while(~ans&1)ans>>=1;
	ptn(ans);
	return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define y0 vjfuyg
#define y1 jfskav
#define pb push_back
#define lson (k<<1)
#define rson (k<<1|1)
#define lowbit(x) ((x)&-(x))
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<" = "<<(x)<<endl
#define rep(i,s,t) for(int i=(s),_t=(t);i<_t;++i)
#define per(i,s,t) for(int i=(t)-1,_s=(s);i>=_s;--i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> veci;
const int inf=0x7fffffff,infty=(int)1e9,mod=(int)1e9+7,dxy[]={-1,0,1,0,-1};
const ll INF=1ll<<60;
const db pi=acos(-1),eps=1e-8;
template<class T>void rd(T &x)
{
	x=0;
	char c;
	while(c=getchar(),c<48);
	do x=x*10+(c^48);
		while(c=getchar(),c>47);
}
template<class T>void pt(T x)
{
	if(!x)
	{
		putchar('0');
		return;
	}
	static int stk[65];
	int tp=0;
	for(;x;x/=10)stk[tp++]=x%10;
	per(i,0,tp)putchar(stk[i]^48);
}
template<class T>inline void pts(T x)
{
	pt(x),putchar(' ');
}
template<class T>inline void ptn(T x)
{
	pt(x),putchar('\n');
}
template<class T>inline void Max(T &a,T b)
{
	if(b>a)a=b;
}
template<class T>inline void Min(T &a,T b)
{
	if(b<a)a=b;
}

const int N=(int)2e5+5;

int par[N],ID,mx_dist;
veci T[N];
bool ban[N];

void dfs(int u,int pre,int dist){
    if(dist>=mx_dist){
        ID=u;
        mx_dist=dist;
    }
    par[u]=pre;
    for(int v:T[u]){
        if(v==pre)continue;
        dfs(v,u,dist+1);
    }
}

bool mark[N];

veci calc(int u,int pre=0)
{
	veci arr;
	for(int v:T[u])
	{
		if(ban[v]||v==pre)continue;
		veci tmp=calc(v,u);
		if(tmp[0]==-1)return {-1};
		for(int elm:tmp)
		{
			arr.pb(elm+1);
			mark[elm+1]=true;
		}
	}
	veci res;
	for(int elm:arr)if(mark[elm])
	{
		mark[elm]=false;
		res.pb(elm);
	}
	if(res.empty())return {0};
	if(siz(res)>2)return {-1};
	return res;
}

int ans;
veci rem[N];

bool check1()
{
	rep(i,0,mx_dist+1)for(int elm:rem[i])
	{
//		debug(elm);//
		if(~elm&&elm&&elm!=i&&elm!=mx_dist-i)return false;
	}
	ans=mx_dist;
	return true;
}

bool check2()
{
	if(mx_dist&1)return false;
	if(siz(rem[mx_dist>>1])>1)return false;
	rep(i,0,mx_dist+1)
	{
		if((i<<1)==mx_dist)continue;
		for(int elm:rem[i])
		{
			if(~elm&&elm&&elm!=i&&elm!=mx_dist-i)return false;
		}
	}
	ans=rem[mx_dist>>1][0]+(mx_dist>>1);
	return true;
}

int main()
{
	int n;
	rd(n);
	for(int i=1,u,v;i<n;++i)
	{
		rd(u),rd(v);
		T[u].pb(v);
		T[v].pb(u);
	}
	mx_dist=0;
    dfs(1,0,0);
    dfs(ID,0,0);
    for(int u=ID;u;u=par[u])ban[u]=true;
	for(int u=ID,i=0;u;u=par[u],++i)rem[i]=calc(u);
	if(!check1()&&!check2())return puts("-1"),0;
//	debug(ans);//
	while(~ans&1)ans>>=1;
	ptn(ans);
	return 0;
}


#include <iostream>
using namespace std ;
int main()
{
int x,n,z,dispress=0,sum=0;
cin>>x>>n;
sum = n;
for (int i=0;i<x;i++){
    cin>>z;
    if (z>0)
        sum += z;
    else if (z<0)
    {
       if((sum + z)<0)
        dispress++;
       else
        sum += z;
    }
}
cout<<sum<<" "<<dispress;
return 0;
}


#include <iostream>
using namespace std ;
int main()
{
int x,n,dispress=0,sum=0,num;
char z;
cin>>x>>n;
sum = n;
for (int i=0;i<x;i++)
{
	cin>>z;
    switch(z)
    {
        case '+':
        cin>>num;
		sum += num;
       	break;
       	case '-':
       	cin>>num;
       	if(num>sum)
       	dispress++;
		else if (num<=sum)
		sum -= num;
	}
}
cout<<sum<<" "<<dispress;
return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
	int n, x;
	cin >> n >> x;
	int count = 0;
	char * c = new char[n];
	int * a = new int[n];
	for (int i = 0; i < n; i++)
	{
		cin >> c[i] >> a[i];
		if (c[i] == '+')
		{
			x += a[i];
		}
		else
		{
			if (a[i] > x)
			{
				count++;
			}
			else
			{
				x -= a[i];
			}
		}
	}
	cout << x << " " << count;
//	system("pause");
}


#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
	int n, x;
	cin >> n >> x;
	int count = 0;
	char * c = new char[n];
	int * a = new int[n];
	for (int i = 0; i < n; i++)
	{
		cin >> c[i] >> a[i];
		if (c[i] == '+')
		{
			x += a[i];
		}
		else
		{
			if (a[i] > x)
			{
				count++;
			}
			else
			{
				x -= a[i];
			}
		}
	}
//	cout << x << " " << count;
	system("pause");
}


#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
	long long n, x;
	cin >> n >> x;
	int count = 0;
	char * c = new char[n];
	long long  * a = new long long[n];
	for (long long  i = 0; i < n; i++)
	{
		cin >> c[i] >> a[i];
		if (c[i] == '+')
		{
			x += a[i];
		}
		else
		{
			if (a[i] > x)
			{
				count++;
			}
			else
			{
				x -= a[i];
			}
		}
	}
	cout << x << " " << count;
//	system("pause");
}


#include <iostream>
using namespace std;
int main()
{
	long long int n, c;
	cin >> n >> c;
	long long int ctr =0;
	int*x = new int[n];
	for (int i = 0;i < n;i++)
		cin >> x[i];
	int it = 0;
	int it2 = 1;
	while (it2 < n)
	{

		if (x[it2] - x[it]<=c)
		{
			if (ctr == 0)
			{
				ctr += 2;
			}
			else
			{
				ctr++;
			}
				it++;
				it2++;
		}
		else
		{
			 it2++;
			 if (ctr == 0)
			 {
				 it++;
			 }
		}
	}
	cout << ctr;

}


#include <iostream>
using namespace std;
int main()
{
	int n, x,k=0;
	cin >> n >> x;
	char*op = new char[n];
	int*ic = new int[n];
	for (int i = 0;i < n;i++)
	{
		cin >> op[i] >> ic[i];
	}
	for (int i = 0;i < n;i++)
	{
		if (op[i] == '+')
		{
			x = x + ic[i];
		}
		if (op[i] == '-')
		{
			if (x - ic[i] < 0)
			{
				k++;
			}
			if (x - ic[i]>= 0)
			{
				x = x - ic[i];
			}
		}
	}
	cout << x << " "<< k<<endl;
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	long int n, x,k=0;
	cin >> n >> x;
	char*op = new char[n];
	int*ic = new int[n];
	for (int i = 0;i < n;i++)
	{
		cin >> op[i] >> ic[i];
	}
	for (int i = 0;i < n;i++)
	{
		if (op[i] == '+')
		{
			x = x + ic[i];
		}
		if (op[i] == '-')
		{
			if (x - ic[i] < 0)
			{
				k++;
			}
			if (x - ic[i]>= 0)
			{
				x = x - ic[i];
			}
		}
	}
	cout << x << " "<< k<<endl;

	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	long long int n, x,k=0;
	cin >> n >> x;
	char*op = new char[n];
	int*ic = new int[n];
	for (int i = 0;i < n;i++)
	{
		cin >> op[i] >> ic[i];
	}
	for (int i = 0;i < n;i++)
	{
		if (op[i] == '+')
		{
			x = x + ic[i];
		}
		if (op[i] == '-')
		{
			if (x - ic[i] < 0)
			{
				k++;
			}
			if (x - ic[i]>= 0)
			{
				x = x - ic[i];
			}
		}
	}
	cout << x << " "<< k<<endl;
	return 0;
}


#include <stdio.h>
int main()
{
	int n, m, i, count, k=0;
	char s[2];
	scanf ("%d%d", &n, &count);
	for (i=0; i<n; i++)
	{
		scanf ("%s%d", &s, &m);
		if (s[0]=='+')
			count+=m;
		else
			if (count<m)
				k++;
			else
				count-=m;
	}
	printf ("%d %d", count, k);
	return 0;
}


#include <stdio.h>
int main()
{
	int n, m, i, count, k=0;
	char s[2];
	scanf ("%d%d", &n, &count);
	for (i=0; i<n; i++)
	{
		scanf ("%s%d", &s, &m);
		if (s[0]=='+')
			count+=m;
		if (s[0]=='-')
		{
			if (count<m)
				k++;
			else
				count-=m;
		}
	}
	printf ("%d %d", count, k);
	return 0;
}


#include <stdio.h>
int main()
{
	int n, m, i, k=0;
	long long count;
	char s[2];
	scanf ("%d%I64d", &n, &count);
	for (i=0; i<n; i++)
	{
		scanf ("%s%d", &s, &m);
		if (s[0]=='+')
			count+=m;
		else
			if (count<m)
				k++;
			else
				count-=m;
	}
	printf ("%I64d %d", count, k);
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
	unsigned long long n, x;
	char c;
	unsigned long long t;
	unsigned long long count = 0;
	cin >> n >> x;

	for (int i = 0; i < n; ++i)
	{
		cin >> c >> t;
		if (c == '-')
		{
			if (x >= t)
			{
				x = x - t;
			}
			else
			{
				++count;
			}
		}
		else
		{
			x = x + t;
		}
	}
	cout << x << ' ' << count;
}


#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
	unsigned n, x;
	char c;
	unsigned t;
	unsigned count = 0;
	cin >> n >> x;

	for (int i = 0; i < n; ++i)
	{
		cin >> c >> t;
		if (c == '-')
		{
			if (x >= t)
			{
				x = x - t;
			}
			else
			{
				++count;
			}
		}
		else
		{
			x = x + t;
		}
	}
	cout << x << ' ' << count;
}


#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
	int n, x;
	char c;
	int t;
	int count = 0;
	cin >> n >> x;

	for (int i = 0; i < n; ++i)
	{
		cin >> c >> t;
		if (c == '-')
		{
			if (x >= t)
			{
				x = x - t;
			}
			else
			{
				++count;
			}
		}
		else
		{
			x = x + t;
		}
	}
	cout << x << ' ' << count;
}


#include <iostream>
#include<cmath>
#include <vector>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main() {

	long long  n, packs, dis = 0;
	char op; 	long long ip;

	cin >> n >> packs;

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

		cin >> op >> ip;

		if (op == '+') packs += ip;
		else if (op == '-') {
			if (packs - ip < 0) dis++;
			else if (packs - ip >= 0) packs -= ip;
		}
	}

	cout << packs << " " << dis;

	return 0;
}


#include <iostream>
#include<cmath>
#include <vector>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main() {

	long long n, packs, dis = 0;
	char op; long long  ip;

	cin >> n >> packs;

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

		cin >> op >> ip;

		if (op == '+') packs += ip;
		else if (op == '-') {
			if (packs - ip < 0) dis++;
			else if (packs - ip >= 0) packs -= ip;
		}
	}

	cout << packs << " " << dis;

	system("pause");

	return 0;
}


#include <iostream>
#include<cmath>
#include <vector>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main() {

	int n, packs, dis = 0;
	char op; int ip;

	cin >> n >> packs;

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

		cin >> op >> ip;

		if (op == '+') packs += ip;
		else if (op == '-') {
			if (packs - ip < 0) dis++;
			else if (packs - ip >= 0) packs -= ip;
		}
	}

	cout << packs << " " << dis;

	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
	int n=0;
	long long int x=0;
	cin >> n >> x;
	char posNeg = ' ';
	int d;
	int counter=0;
	for(int i=0;i<n;i++){
		cin >> posNeg >> d;
		if(posNeg == '+')
			x+=d;
		else if (posNeg == '-')
			x-=d;

		if(x < 0){
			x+=d;
			counter+=1;
		}
	}

	cout << x << ' ' << counter << endl;

    return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
	int n=0;
	int x=0;
	cin >> n >> x;
	char posNeg = ' ';
	int d;
	int counter=0;
	for(int i=0;i<n;i++){
		cin >> posNeg >> d;
		if(posNeg == '+')
			x+=d;
		else if (posNeg == '-')
			x-=d;

		if(x < 0){
			x+=d;
			counter+=1;
		}
	}

	cout << x << ' ' << counter << endl;

    return 0;
}


#include <iostream>
#include <cmath>
#include <stack>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

long long n, x, d;
long long gr=0;
char c;

int main()
{
 cin>>n>>x;
 for (int i=0; i<n;++i)
 {cin>>c>>d;
  if(c=='+') x+=d;
  else if(c=='-'&&d<=x) x-=d;
  else ++gr;}
  cout<<x<<" "<<gr;
}


#include <iostream>

using namespace std;

int n, x, d;
int gr=0;
char c;

int main()
{
 cin>>n>>x;
 for (int i=0; i<n;++i)
 {cin>>c>>d;
  if(c=='+') x+=d;
  else if(c=='-'&&d<x) x-=d;
  else ++gr;}
  cout<<x<<" "<<gr;
}


#include <iostream>
#include <cmath>
#include <stack>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

int n, x, d;
int gr=0;
char c;

int main()
{
 cin>>n>>x;
 for (int i=0; i<n;++i)
 {cin>>c>>d;
  if(c=='+') x+=d;
  else if(c=='-'&&d<=x) x-=d;
  else ++gr;}
  cout<<x<<" "<<gr;
}


#include <iostream>

using namespace std;

int main(int argc, char** argv) {
	long n, i, x, a, k = 0;
	char c;
	cin>>n>>x;
	long s = x;
	for(i = 1;i<=n;i++){
		cin>>c>>a;
		if(c == '+'){
			s+=a;
		}else{
			if(s>=a){
				s-=a;
			}else{
				k++;
			}
		}
	}
	cout<<s<<" "<<k;
	return 0;
}


#include <iostream>

using namespace std;

int main(int argc, char** argv) {
	long long n, i, x, a, k = 0;
	char c;
	cin>>n>>x;
	long long s = x;
	for(i = 1;i<=n;i++){
		cin>>c>>a;
		if(c == '+'){
			s+=a;
		}else{
			if(s>=a){
				s-=a;
			}else{
				k++;
			}
		}
	}
	cout<<s<<" "<<k;
	return 0;
}


#include <iostream>

using namespace std;

int main(int argc, char** argv) {
	int n, i, x, a, k = 0;
	char c;
	cin>>n>>x;
	int s = x;
	for(i = 1;i<=n;i++){
		cin>>c>>a;
		if(c == '+'){
			s+=a;
		}else{
			if(s>=a){
				s-=a;
			}else{
				k++;
			}
		}
	}
	cout<<s<<" "<<k;
	return 0;
}


#include <iostream>

using namespace std;

int main()
{
	int n, x;
	cin >> n >> x;

	long long cnt = x, sad_cnt = 0;
	for (int i = 0; i < n; i++) {
		char t_sign;
		int t_cnt;
		cin >> t_sign >> t_cnt;

		if (t_sign == '+')
			cnt += t_cnt;
		else {
			if (cnt - t_cnt >= 0)
				cnt -= t_cnt;
			else
				sad_cnt++;
		}
	}

	cout << cnt << " " << sad_cnt << endl;
}


#include <iostream>

using namespace std;

int main()
{
	int n, x;
	cin >> n >> x;

	int cnt = x, sad_cnt = 0;
	for (int i = 0; i < n; i++) {
		char t_sign;
		int t_cnt;
		cin >> t_sign >> t_cnt;

		if (t_sign == '+')
			cnt += t_cnt;
		else
			if (cnt - t_cnt >= 0)
				cnt -= t_cnt;
			else
				sad_cnt++;
	}

	cout << cnt << " " << sad_cnt << endl;
}


#include<iostream>
#include<string>

using namespace std;

long long int n, x, i, a, cnt;
char str;

int main() {

	cin >> n >> x;

	for (i = 0; i < n; i++)
	{
		cin >> str >> a;

		if (str == '+')
		{
			x += a;
		}

		if (str == '-')
		{
			if (x >= a)
			{
				x -= a;
			}
			else
			{
				cnt++;
			}
		}

	}

	cout << x << " " << cnt;

	return 0;
}


#include<iostream>
#include<string>

using namespace std;

long long int n,x,i,a[1000],cnt;
string str;

int main() {

    cin >> n >> x;

    for(i=0 ; i < n ; i++) cin >> str[i] >> a[i];

    for(i=0 ; i < n ; i++)
    {
        if(str[i] == '+')
        {
            x += a[i];
        }
        if(str[i] == '-' && x >= a[i])
        {
            x-= a[i];
        }
        else
        {
            cnt++;
        }
    }

    cout << x << " " << cnt;

    return 0;
}


#include<iostream>
#include<string>

using namespace std;

long long int n,x,i,a[1000],cnt;
string str;

int main() {

    cin >> n >> x;

    for(i=0 ; i < n ; i++) cin >> str[i] >> a[i];

    for(i=0 ; i < n ; i++)
    {
        if(str[i] == '+')
        {
            x += a[i];
        }
        if(str[i] == '-' )
        {
            if(x >= a[i])
            {
                x-= a[i];
            }
            else
            {
                cnt++;
            }
        }

    }

    cout << x << " " << cnt;

    return 0;
}


#include <iostream>
using namespace std;

int main() {
	int a,b,t,n=0;
	char s;
	cin >> a >> b;
	for (int i=1;i<=a;i++){
		cin >> s >> t;
		if (s=='+'){
			b=b+t;
		}else if(b>t){
			b=b-t;
		}else {
			n++;
		}
	}
	cout << b <<' '<< n;
	return 0;
}


#include <iostream>
using namespace std;

int main() {
	int a,b,t,n=0;
	char s;
	cin >> a >> b;
	for (int i=1;i<=a;i++){
		cin >> s >> t;
		if (s=='+'){
			b=b+t;
		}else if(b>=t){
			b=b-t;
		}else {
			n++;
		}
	}
	cout << b <<' '<< n;
	return 0;
}


#include <iostream>
using namespace std;

int main() {
	int a,t,n=0;
	long long b;
	char s;
	cin >> a >> b;
	for (int i=1;i<=a;i++){
		cin >> s >> t;
		if (s=='+'){
			b=b+t;
		}else if(b>=t){
			b=b-t;
		}else {
			n++;
		}
	}
	cout << b <<' '<< n;
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <queue>
using namespace std;

vector <long long> a;
vector<vector<pair<long long, long long>>> p;
vector <bool> visited;
long long n, x;

long long answer = 0;

void dfs(long long v, bool del, long long cur_sum) {
	if (del) answer++;

	if (!del && cur_sum > a[v]) {
		answer++;
		del = true;
	}

	for (int i = 0; i < p[v].size(); i++)
		if (cur_sum + p[v][i].second > 0)
			dfs(p[v][i].first, del, cur_sum+ p[v][i].second);
		else
			dfs(p[v][i].first, del, 0);
}

void main() {
	FILE *stream;
	//freopen_s(&stream, "input.txt", "r", stdin);
	int ans = 0;
	cin >> n >> x;

	for (int i = 0; i < n; i++) {
		char c;
		int a;
		cin >> c >> a;
		if (c == '+') x += a;
		else
			if (x > a) x -= a;
			else
				ans++;
	}
	cout << x << " " << ans;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <queue>
using namespace std;

long long n, x;

void main() {
	FILE *stream;
	//freopen_s(&stream, "input.txt", "r", stdin);
	int ans = 0;
	cin >> n >> x;

	for (int i = 0; i < n; i++) {
		char c;
		long long int a;
		cin >> c >> a;
		if (c == '+') x += a;
		else {
			if (x >= a) x -= a;
			else
				ans++;
		}

	}
	cout << x << " " << ans;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <queue>
using namespace std;

vector <long long> a;
vector<vector<pair<long long, long long>>> p;
vector <bool> visited;
long long n, x;

long long answer = 0;

void dfs(long long v, bool del, long long cur_sum) {
	if (del) answer++;

	if (!del && cur_sum > a[v]) {
		answer++;
		del = true;
	}

	for (int i = 0; i < p[v].size(); i++)
		if (cur_sum + p[v][i].second > 0)
			dfs(p[v][i].first, del, cur_sum+ p[v][i].second);
		else
			dfs(p[v][i].first, del, 0);
}

void main() {
	FILE *stream;
	//freopen_s(&stream, "input.txt", "r", stdin);
	int ans = 0;
	cin >> n >> x;

	for (int i = 0; i < n; i++) {
		char c;
		long long int a;
		cin >> c >> a;
		if (c == '+') x += a;
		else
			if (x > a) x -= a;
			else
				ans++;
	}
	cout << x << " " << ans;
}


#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <bitset>
#include <string>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <functional>
#include <set>
#include <list>
#include <algorithm>
using namespace std;

typedef long long int ll;

int main() {
	ll  x;
	int n;
	int d = 0;
	scanf("%d %I64d", &n, &x);
	for (int i = 0; i < n; i++) {
		char c;
		ll q;
		cin >> c;
		scanf("%I64d", &q);
		if (c == '+')
			x += q,
			d++;
		else {
			if (x > q)
				x -= q,
				d++;
		}
	}
	printf("%I64d %d\n", x, n - d);
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <bitset>
#include <string>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <functional>
#include <set>
#include <list>
#include <algorithm>
using namespace std;

typedef long long int ll;

int main() {
	ll  x;
	int n;
	int d = 0;
	scanf("%d %I64d", &n, &x);
	for (int i = 0; i < n; i++) {
		char c;
		ll q;
		cin >> c >> q;
		if (c == '+')
			x += q;
		else {
			if (x > q)
				x -= q;
			else
				d++;
		}
	}
	printf("%I64d %d\n", x, d);
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <bitset>
#include <string>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <functional>
#include <set>
#include <list>
#include <algorithm>
using namespace std;

typedef long long int ll;

int main() {
	int n;
	int d = 0;
	ll x;
	cin >> n >> x;
	for (int i = 0; i < n; i++) {
		char c;
		cin >> c;
		ll q;
		cin >> q;
		if (c == '+')
			x += q;
		else {
			if (x >= q)
				x -= q;
			else
				d++;
		}
	}
	cout << x << ' ' << d << endl;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <bitset>
#include <string>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stack>
#include <queue>
#include <functional>
#include <set>
#include <list>
#include <algorithm>
using namespace std;

typedef long long int ll;

int main() {
	int n;
	int d = 0;
	ll x;
	cin >> n >> x;
	for (int i = 0; i < n; i++) {
		char c;
		cin >> c;
		ll q;
		cin >> q;
		if (c == '+')
			x += q;
		else {
			if (x > q)
				x -= q;
			else
				d++;
		}
	}
	cout << x << ' ' << d << endl;
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	int number, pack,ice,kid=0;
	char mark;
	cin >> number >> pack;
	for (int c = 0; c < number; c++){
		cin >> mark >> ice;
		if (mark == '+')
			pack += ice;
		if (mark == '-')
			if (pack >= ice)
				pack -= ice;
			else kid++;
	}
	cout << pack << ' ' << kid;
}


#include <iostream>
using namespace std;
int main()
{
	long long int  pack, ice;
	int number, kid = 0;
	char mark;
	cin >> number >> pack;
	for (int c = 0; c < number; c++){
		cin >> mark >> ice;
		if (mark == '+')
			pack += ice;
		else if (pack >= ice)
			pack -= ice;
		else kid++;
	}
	cout << pack << ' ' << kid;
}


#include <iostream>
#include <cmath>
#include <string>
using namespace std;

int main()
{
	int n,x;
	cin>>n>>x;
	char c;
	int number;
	int D = 0;
	for(int i=0;i<n;i++)
	{
		cin>>c;
		cin>> number;
		if(c == '+')
			x+=number;
		else if(c == '-')
		{
			if(x >= number)
				x-=number;
			else
				D++;
		}
	}
	cout<<x<<" "<<D<<endl;
	return 0;
}


#include <iostream>
#include <cmath>
#include <string>
using namespace std;

int main()
{
	int n;
	long long x;

	cin>>n>>x;
	char c;
	int number;
	int D = 0;
	for(int i=0;i<n;i++)
	{
		cin>>c;
		cin>> number;
		if(c == '+')
			x+=number;
		else if(c == '-')
		{
			if(x >= number)
				x-=number;
			else
				D++;
		}
	}
	cout<<x<<" "<<D<<endl;
	return 0;
}


#include <iostream>
#include <cmath>
#include <string>
using namespace std;

int main()
{
	int n;
	double x;

	cin>>n>>x;
	char c;
	int number;
	int D = 0;
	for(int i=0;i<n;i++)
	{
		cin>>c;
		cin>> number;
		if(c == '+')
			x+=number;
		else if(c == '-')
		{
			if(x >= number)
				x-=number;
			else
				D++;
		}
	}
	cout<<x<<" "<<D<<endl;
	return 0;
}


#include <iostream>
#include<string>
#include<sstream>
#include<cmath>
#include<algorithm>

using namespace std;

int main()
{
	int N;
	int iSum, iCount = 0, C;
	char O;
	cin >> N >> iSum;
	for (int i = 0;i < N;i++)
	{
		cin >> O >> C;
		if (O == '+')
			iSum += C;
		else if (O == '-')
		{
			if (iSum < C)
				iCount++;
			else
				iSum -= C;
		}
	}
	cout << iSum << " " << iCount << endl;
}


#include <iostream>
#include<string>
#include<sstream>
#include<cmath>
#include<algorithm>

using namespace std;

int main()
{
	int N;
	long long iSum;
	int iCount = 0, C;
	char O;
	cin >> N >> iSum;
	for (int i = 0;i < N;i++)
	{
		cin >> O >> C;
		if (O == '+')
			iSum += C;
		else if (O == '-')
		{
			if (iSum < C)
				iCount++;
			else
				iSum -= C;
		}
	}
	cout << iSum << " " << iCount << endl;
}


#include <iostream>
using namespace std;
int main() {
	long x = 0,a=0;
	int d = 0, kids = 0;
	cin >> d>>x;
	char c= '+';
	for (size_t i = 0; i < d; i++)
	{
		cin >> c >> a;
		if (c == '+')
			x += a;
		else {
			if (x >= a)
				x -= a;
			else
				kids++;
		}
	}
	cout << x << " " << kids << endl;
}


#include <iostream>
using namespace std;
int main() {
	long long x = 0,a=0;
	int d = 0, kids = 0;
	cin >> d>>x;
	char c= '+';
	for (size_t i = 0; i < d; i++)
	{
		cin >> c >> a;
		if (c == '+')
			x += a;
		else {
			if (x >= a)
				x -= a;
			else
				kids++;
		}
	}
	cout << x << " " << kids << endl;
}


#include <iostream>
using namespace std;
int main() {
	int x = 0,d=0,a=0,kids=0;
	cin >> d>>x;
	char c= '+';
	for (size_t i = 0; i < d; i++)
	{
		cin >> c >> a;
		if (c == '+')
			x += a;
		else {
			if (x >= a)
				x -= a;
			else
				kids++;
		}
	}
	cout << x << " " << kids << endl;
}


#include<stdio.h>
#include<math.h>
#define max(a,b) a>b?a:b
int main()
{
   long long int n ,x,a[1000],count=0;
    char s[1001];
    scanf("%d%d",&n,&x);
    for(int i=0;i<n;i++)
    {
    	scanf("%s%d",&s[i],&a[i]);
    	if(s[i]=='-'&&a[i]>x)
    	{
    		count++;
		}
		if(s[i]=='-'&&a[i]<=x)
    	{
    	    x-=a[i];
		}
		if(s[i]=='+')
		{
			x+=a[i];
		}
	}
	printf("%I64d %I64d",x,count);
}


#include<stdio.h>
#include<math.h>
#define max(a,b) a>b?a:b
int main()
{
   long long int n ,x,a[1000],count=0;
    char s[1001];
    scanf("%d%d",&n,&x);
    for(int i=0;i<n;i++)
    {
    	scanf("%s%d",&s[i],&a[i]);
    	if(s[i]=='-'&&a[i]>x)
    	{
    		count++;
		}
		if(s[i]=='-'&&a[i]<=x)
    	{
    	    x-=a[i];
		}
		if(s[i]=='+')
		{
			x+=a[i];
		}
	}
	printf("%lld %lld",x,count);
}


#include<stdio.h>
#include<math.h>
#define max(a,b) a>b?a:b
int main()
{
   long long int n ,x,a,count=0;
    char s[2];
    scanf("%I64d%I64d",&n,&x);
    for(int i=0;i<n;i++)
    {
    	scanf("%s%I64d",&s[0],&a);
    	if(s[0]=='-'&&a>x)
    	{
    		count++;
		}
		if(s[0]=='-'&&a<=x)
    	{
    	    x-=a;
		}
		if(s[0]=='+')
		{
			x+=a;
		}
	}
	printf("%I64d %I64d\n",x,count);
}


#include<stdio.h>
#include<math.h>
#define max(a,b) a>b?a:b
int main()
{
   long long int n ,x,a,count=0;
    char s;
    scanf("%d%d",&n,&x);
    for(int i=0;i<n;i++)
    {
    	scanf("%s%d",&s,&a);
    	if(s=='-'&&a>x)
    	{
    		count++;
		}
		if(s=='-'&&a<=x)
    	{
    	    x-=a;
		}
		if(s=='+')
		{
			x+=a;
		}
	}
	printf("%I64d %I64d",x,count);
}


#include<iostream>
#include<algorithm>
#include<fstream>
#include<utility>
#include<cstring>
#include<string>
#include<vector>
#include<math.h>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<queue>
using namespace std;

int main()
{

	long long n,x,d,cd;
	char ch;
	cin>>n>>x;
	for(int i =0;i<n;i++)
	{
		cin>>ch>>d;
		if(ch=='+')
			x+=d;
		else
		{
			if(x>=d)
				x-=d;
			else
				cd++;
		}
	}
	cout<<x<<" "<<cd<<endl;
	return 0;
}


#include<iostream>

using namespace std;

int main()
{

	long long n,x,d=0,cd=0;
	char ch;
	cin>>n>>x;
	for(int i =0;i<n;i++)
	{
		cin>>ch>>d;
		if(ch=='+')
			x+=d;
		else
		{
			if(x>=d)
				x-=d;
			else
				cd++;
		}
	}
	cout<<x<<' '<<cd;
	return 0;
}


#include<iostream>

using namespace std;

int main()
{

	long long n,x,d,cd;
	char ch;
	cin>>n>>x;
	for(int i =0;i<n;i++)
	{
		cin>>ch>>d;
		if(ch=='+')
			x+=d;
		else
		{
			if(x>=d)
				x-=d;
			else
				cd++;
		}
	}
	cout<<x<<" "<<cd;
	return 0;
}


#include<iostream>
using namespace std;

void main()
{
    long n, x;
    cin >> n >> x;
    long long total = x;
    long sad = 0;

    for(long i = 0; i < n; i ++)
    {
        char note;
        long a;
        cin >> note >> a;

        if(note == '+')
        {
            total += a;
        }
        else
        {
            if (total >= a)
            {
                total -= a;
            }
            else
            {
                sad ++;
            }
        }
    }

    cout << total << " " << sad << endl;
}


#include<iostream>
using namespace std;

void main()
{
    long n, x;
    cin >> n >> x;
    long total = x, sad = 0;
    for(long i = 0; i < n; i ++)
    {
        char note;
        long a;
        cin >> note >> a;

        if(note == '+')
        {
            total += a;
        }
        else
        {
            if (total >= a)
            {
                total -= a;
            }
            else
            {
                sad ++;
            }
        }
    }

    cout << total << " " << sad << endl;
}


#include<iostream>
using namespace std;

void main()
{
    long n, x;
    cin >> n >> x;
    long total = x, sad = 0;
    while(n --)
    {
        char note;
        long a;
        cin >> note >> a;

        if(note == '+')
        {
            total += a;
        }
        else
        {
            if (total >= a)
            {
                total -= a;
            }
            else
            {
                sad ++;
            }
        }
    }

    cout << total << " " << sad << endl;
}


#include <iostream>

using namespace std;

int main()
{
	double n, x, d2[1000];
	char d1[1000];
	cin >> n >> x;
	for (int i = 0; i < n; i++)
	{
		cin >> d1[i] >> d2[i];
	}
	int smutek = 0;
	for (int i = 0; i < n; i++)
	{
		if (d1[i] == '+')
			x += d2[i];
		if (d1[i] == '-')
		{
			if (x < d2[i])
				smutek++;
			else
				x -= d2[i];
		}
	}
	cout << x << " " << smutek;
	return 0;
}


#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	double  n, x, d2[1000];
	char d1[1000];
	cin >> n >> x;
	int smutek = 0;
	for (int i = 0; i < n; i++)
	{

		cin >> d1[i] >> d2[i];
		if (d1[i] == '+')
			x += d2[i];
		if (d1[i] == '-')
		{
			if (x < d2[i])
				smutek++;
			else
				x -= d2[i];
		}
	}
	cout << setprecision(1000000000) << x << " " << smutek;
	return 0;
}


#include <iostream>

using namespace std;

int main()
{
	int n, x, d2[1000];
	char d1[1000];
	cin >> n >> x;
	for (int i = 0; i < n; i++)
	{
		cin >> d1[i] >> d2[i];
	}
	int smutek = 0;
	for (int i = 0; i < n; i++)
	{
		if (d1[i] == '+')
			x += d2[i];
		if (d1[i] == '-')
		{
			if (x < d2[i])
				smutek++;
			else
				x -= d2[i];
		}
	}
	cout << x << " " << smutek;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

inline int in() { int x; scanf("%d", &x); return x; }
const int N = 202;

int a[N][N];

int main()
{
	int n = in(), d = in();
	int ans = 0, cur = 0;
	for(int i = 0; i < d; i++)
	{
		string s;
		cin >> s;
		bool iff = 0;
		for(int j = 0; j < n; j++)
			iff |= (s[j] == '0');
		if(iff)
			cur++;
		else
			cur = 0;
		ans = max(ans, cur);
	}
	cout << ans << endl;
}


#include <iostream>
using namespace std;
int main()
{
    long long int n,x,c,d;
    char h;
    cin>>n>>x;
    for(int i=0;i<n;i++)
    {
        cin>>h>>d;
        if(h=='+')
        x+=d;
        else
        {
            if(x>=d)
            {
                x-=d;
            }
            else
            c++;
        }
    }

    cout<<x<<" "<<c<<endl;
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int n,x,c,d;
    char h;
    cin>>n>>x;
    for(int i=0;i<n;i++)
    {
        cin>>h>>d;
        if(h=='+')
        x+=d;
        else
        {
            if(x>=d)
            {
                x-=d;
            }
            else
            c++;
        }
    }

    cout<<x<<" "<<c<<endl;
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int n,x,c=0,d;
    char h;
    cin>>n>>x;
    for(int i=0;i<n;i++)
    {
        cin>>h>>d;
        if(h=='+')
        x+=d;
        else
        {
            if(x>=d)
            {
                x-=d;
            }
            else
            c++;
        }
    }

    cout<<x<<" "<<c<<endl;
    return 0;
}