#include<iostream>
#include<cstring>
using namespace std;
int n=1;
int a[201][73],l[201],r[201],b[201],c[201];
void buildtree(int x,int lr)
{
n++;
cin>>b[n]>>c[n];
if(lr==1)l[x]=n;else r[x]=n;
int n1=n;
if(c[n]==0)
{
buildtree(n1,1);
buildtree(n1,2);
}
}
void dfs(int i)
{
if(l[i]==0&&r[i]==0)
{
a[i][0]=0;
for(int j=1;j<=c[i];j++)
a[i][j]=5*j+b[i]*2;
}
else
{
dfs(l[i]);
dfs(r[i]);
for(int j=0;j<=72;j++)
for(int k=0;k<=72-j;k++)
if(a[i][j+k]>a[l[i]][j]+a[r[i]][k]+b[i]*2)
a[i][j+k]=a[l[i]][j]+a[r[i]][k]+b[i]*2;
}
}
int main()
{
int t;
cin>>t;
buildtree(1,1);
memset(a,2e9,sizeof(a));
dfs(2);
int i=72;
while(a[2][i]>t)i--;
cout<<i;
return 0;
}