#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e3;
int p[N],c[N];
vector<int> ma[N];
int dp[N][309];
// ll ans[N],offset[N];
// priority_queue<int> lt[N];
// priority_queue<int,vector<int>,greater<int>> rt[N];
// void merge(int x,int y)
// {
// ans[x]+=ans[y];
// while(lt[y].size())
// {
// int v=lt[y].top()+offset[y];
// int rtp=rt[x].top()+offset[x];
// lt[y].pop();
// if(v<=rtp)
// {
// lt[x].push(v-offset[x]);
// }
// else
// {
// ans[x]+=(v-rtp);
// lt[x].push(rtp);
// rt[x].pop();
// rt[x].push(v-offset[x]);
// }
// }
// while(rt[y].size())
// {
// int v=rt[y].top()+offset[y];
// int ltp=lt[x].top()+offset[x];
// rt[y].pop();
// if(ltp<=v)
// {
// rt[x].push(v-offset[x]);
// }
// else
// {
// ans[x]+=(ltp-v);
// rt[x].push(ltp);
// lt[x].pop();
// lt[x].push(v-offset[x]);
// }
// }
// }
// void dfs(int x)
// {
// if(ma[x].size()==0)
// {
// lt[x].push(c[x]);
// rt[x].push(c[x]);
// ans[x]=0;
// offset[x]=0;
// return;
// }
// bool f=1;
// lt[x].push(-1e9);
// rt[x].push(1e9);
// ans[x]=0;
// offset[x]=0;
// for(auto y:ma[x])
// {
// dfs(y);
// merge(x,y);
// }
// offset[x]+=c[x];
// cout<<"At "<<x<<' '<<ans[x]<<' '<<offset[x]<<endl;;
// cout<<"LT: ";
// vector<int> out;
// // for(auto y:lt[x])
// while(lt[x].size())
// {
// out.push_back(lt[x].top());
// lt[x].pop();
// }
// for(auto y:out)
// cout<<y<<' ',lt[x].push(y);
// cout<<endl;
// out.clear();
// while(rt[x].size())
// {
// out.push_back(rt[x].top());
// rt[x].pop();
// }
// cout<<"Rt ";
// for(auto y:out)
// cout<<y<<' ',rt[x].push(y);
// cout<<endl;
// }
void dfs1(int x)
{
if(ma[x].size()==0)
{
for(int j=0;j<309;j++)
{
dp[x][j]=abs(c[x]-j);
}
return;
}
for(auto y:ma[x])
dfs1(y);
for(int j=c[x];j<309;j++)
{
dp[x][j]=0;
for(auto y:ma[x])
{
dp[x][j]+=dp[y][j-c[x]];
}
// cout<<x<<' '<<dp[x][j]<<endl;
}
for(int j=0;j<309;j++)
{
for(int k=0;k<309;k++)
{
dp[x][j]=min(dp[x][j],dp[x][k]+abs(k-j));
}
}
}
void solve()
{
int n,m;
cin>>n>>m;
for(int i=2;i<=n+m;i++)
{
cin>>p[i]>>c[i];
ma[p[i]].push_back(i);
for(int j=0;j<309;j++)
{
dp[i][j]=1e9;
}
}
for(int j=0;j<309;j++)
{
dp[1][j]=1e9;
} // dfs(1);
// cout<<ans[1]<<endl;
dfs1(1);
int ans=1e9;
for(int j=0;j<309;j++)
{
ans=min(ans,dp[1][j]);
}
cout<<ans<<endl;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--)solve();
}