#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define all(x) x.first(),x.second()
#define MAXN 100005
typedef long long ll;
using namespace std;
ll n,m;
ll niz[MAXN];
ll duz[MAXN];
ll lep[MAXN];
bool ima[MAXN];
ll ciklus[MAXN];
ll obidjen[MAXN];
ll duzc[MAXN],nagc[MAXN];
vector<ll> ciklusi[MAXN];
vector<ll> parcput[MAXN],parclep[MAXN];

ll dfs(ll t,ll dub){
    ll res=lep[t];
    if(ciklus[t]){
        ll slavija=(m-dub)/(duzc[ciklus[t]]);
        dub+=slavija*(duzc[ciklus[t]]);
        res+=slavija*(nagc[ciklus[t]]);
    }


    if(dub+duz[t]<=m)res+=dfs(niz[t],dub+duz[t]);
    return res;
}

ll dfsc(ll t,ll bc){
    obidjen[t]=1;
    if(obidjen[niz[t]]==2){
        obidjen[t]=2;
        return 0;
    }
    if(obidjen[niz[t]]==1){
        obidjen[t]=2;
        ciklus[t]=bc;
        ciklusi[bc].pb(t);
        duzc[bc]+=duz[t];
        nagc[bc]+=lep[t];
        return niz[t];
    }

    ll lol=dfsc(niz[t],bc);
    obidjen[t]=2;
    if(lol==0)return 0;
    if(t==lol || obidjen[lol]==1){
        ciklus[t]=bc;
        ciklusi[bc].pb(t);
        duzc[bc]+=duz[t];
        nagc[bc]+=lep[t];
        return lol;
    }else return lol;





}

int main()
{
    scanf("%lld %lld", &n, &m);
    for(ll i=1;i<=n;i++)
        scanf("%lld",lep+i);
    for(ll i=1;i<=n;i++)
        scanf("%lld",niz+i);
    for(ll i=1;i<=n;i++)
        scanf("%lld",duz+i);

    for(ll i=1;i<=n;i++)
        ima[niz[i]]=true;


    ll ucc=1;
    for(ll i=1;i<=n;i++){
        ll haha;
        if(obidjen[i])continue;
        haha=dfsc(i,ucc);
        if(haha)ucc++;
    }


    if(n<=1000){
        ll res=0;
        for(ll i=1;i<=n;i++)res=max(res,dfs(i,0));
        printf("%lld",res);
        return 0;
    }
    for(ll i=1;i<ucc;i++){
        ll pdosad=0,ldosad=0;
        for(auto x:ciklusi[i]){
            pdosad+=duz[x];
            ldosad+=lep[x];
            parcput[i].pb(pdosad);
            parclep[i].pb(ldosad);
        }
        for(auto x:ciklusi[i]){
            pdosad+=duz[x];
            ldosad+=lep[x];
            parcput[i].pb(pdosad);
            parclep[i].pb(ldosad);
        }
    }
    printf("2");
    return 0;
    ll res=0;
    for(ll j=1;j<ucc;j++){
        for(ll i=1;i<=duzc[j];i++){
            ll ttres=(m/duzc[j])*nagc[j];
            ll ogr=m-(m/duzc[j])*duzc[j];
            ll l=i;
            ll d=i+n-1;
            ll tres=0;
            while(l<=d){
                ll mid=(l+d)/2;
                if(parcput[j][mid]-parcput[j][i-1]<=ogr){
                    tres=parclep[j][mid]-parclep[j][i-1];
                    l=mid+1;
                }else{
                    d=mid-1;
                }

            }
            res=max(res,ttres+tres);

        }
    }
    printf("%lld",res);


    return 0;
}