#include<bits/stdc++.h>
#define pii pair<int,int>
#define eps 1e-7
#define equals(a,b) (fabs(a - b) < eps)
#define fi first
#define se second
using namespace std;
typedef long long ll;

const int maxn = 1e6 + 5;
const ll MOD = 1e9 + 7;

ll rd(){
	ll x = 0;
	int f = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9'){
		if(ch == '-')  f = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9'){
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	return x * f;
}



struct PSegmentTree{
	struct Node{
		int l,r,d;
		Node *ls,*rs;
	};
	int a[maxn];
	Node pool[maxn * 30],*rt[maxn];
	int top;
	Node* build(int l,int r){
		Node *p = pool + (++top);
		p -> l = l,p -> r = r;
		if(l == r) {
			p -> d = a[l];
			return p;
		}
		int mid = l + r >> 1;
		p -> ls = build(l,mid);
		p -> rs = build(mid + 1,r);
		return p;
	}
	Node* copyNode(Node* rt){
		Node* p = pool + (++top);
		pool[top] = *rt;
		return p;
	}
	Node* change(Node* rt,int id,int x){
		Node* p = copyNode(rt);
		if(p -> l == p -> r) {
			p -> d = x;
			return p;
		}
		int mid = p -> l + p -> r >> 1;
		if(id <= mid) p -> ls = change(p -> ls,id,x);
		else p -> rs = change(p -> rs,id,x);
		return p;
	}
	int query(Node* rt,int id){
		if(rt -> l == rt -> r) {
			return rt -> d;
		}
		int mid = rt -> l + rt -> r >> 1;
		if(id <= mid) return query(rt -> ls,id);
		else return query(rt -> rs,id); 
	}
};

struct Edge{
	int to;
	ll t,k;
};

int main(){
	int n = rd();
	int m = rd();
	int x = rd();
	int y = rd();
	x--;
	y--;
	vector<vector<Edge>> e(n);
	while(m--){
		int a = rd();
		int b = rd();
		int c = rd();
		int d = rd();
		a--;
		b--;
		e[a].push_back({b,c,d});
		e[b].push_back({a,c,d});
	}
	
	vector<ll> dis(n,1e18);
	dis[x] = 0;
	priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> q;
	q.emplace(0,x);
	while(!q.empty()){
		ll d = q.top().fi;
		int u = q.top().se;
		q.pop();
		if(dis[u] != d) continue;
		for(auto v:e[u]) {
			ll tmp = ((d + v.k - 1) / v.k) * v.k + v.t;
			if(tmp < dis[v.to]) {
				dis[v.to] = tmp;
				q.emplace(tmp,v.to);
			}
		}
	}
	if(dis[y] == 1e18) cout << -1 << '\n';
	else cout << dis[y];
}
#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define p_q priority_queue
#define m_p make_pair
#define pb push_back
#define ld long double
#define F first
#define S second
#define ull unsigned long long
#define mod 1000000007
#define md 998244353
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++)
#define int ll
using namespace std;
inline void read(int &x)
{
	short negative=1;
    x=0;
    char c=getchar();
    while(c<'0' || c>'9')
    {
		if(c=='-')
			negative=-1;
		c=getchar();
	}
    while(c>='0' && c<='9')
        x=(x<<3)+(x<<1)+(c^48),c=getchar();
    x*=negative;
}
ll qpow(ll n,ll k){
	ll ans=1;
	while(k){
		if(k%2){
			ans*=n;
			ans%=mod;
		}
		n*=n;
		n%=mod;
		k/=2;
	}
	return ans;
}
string iots(int n)
{
	string s="";
	while(n)
	{
		int now=n%10;
		s+=now+'0';
		n/=10;
	}
	reverse(s.begin(),s.end());
	return s;
}
int stoi(string s)
{
	int n=0;
	rep(i,s.size())
	{
		n*=10;
		n+=s[i]-'0';
	}
	return n;
}
int n;
int val[100100];
vector<int>sons[100100];
int sz[100100];
void dfs(int u)
{
	val[u]=1;
	sz[u]=1;
	rep(i,sons[u].size())
	{
		dfs(sons[u][i]);
		sz[u]+=sz[sons[u][i]];
	}
	int sum=0;
	vector<int>vv;
	vv.clear();
	rep(i,sons[u].size())
	{
		int v=sons[u][i];
		if(sz[v]%2==0)
		{
			if(val[v]<0)
			{
				val[u]+=val[v];
			}
			else
			{
				sum+=val[v];
			}
		}	
		else vv.pb(val[v]);
	}
	sort(vv.begin(),vv.end());
	if(vv.size()%2==0)
	{
		val[u]+=sum;
	}
	else
	{
		val[u]-=sum;
	}
	rep(i,vv.size())
	{
		if(i%2==0)
		{
			val[u]+=vv[i];
		}
		else
		{
			val[u]-=vv[i];
		}
	}
}
signed main()
{
	ios::sync_with_stdio(0);
	cin>>n;
	rep(i,n-1)
	{
		int fa;
		cin>>fa;
		fa--;
		int u=i+1;
		sons[fa].pb(u);
	}
	dfs(0);
	cout<<(val[0]+n)/2<<endl;
	return 0;
}
