#include<bits/stdc++.h>usingnamespace std;constint N =1e5+10;struct Res {int x, y, z;Res(int _x =0,int _y =0,int _z =0):x(_x),y(_y),z(_z){}booloperator<(const Res &t)const{return x < t.x;}}a[N];int l[N], r[N], n, k;int root[N], ls[N <<8], rs[N <<8], sum[N <<8], cnt;int A[50], B[50], cnt1, cnt2;inlineintlowbit(int x){return x &(-x);}voidupdate(int&rt,int l,int r,int x,int v){if(!rt){rt =++cnt;}sum[rt]+= v;if(l == r){return;}int mid = l + r >>1;if(x <= mid){update(ls[rt], l, mid, x, v);}if(x > mid){update(rs[rt], mid +1, r, x, v);}}intquery_sum(int l,int r,int L,int R){if(l >= L && r <= R){int ans =0;for(int i =1; i <= cnt1; i++){ans -= sum[A[i]];}for(int i =1; i <= cnt2; i++){ans += sum[B[i]];}return ans;}int mid = l + r >>1, ans =0, A1[50], B1[50];if(L <= mid){for(int i =1; i <= cnt1; i++){A1[i]= A[i];A[i]= ls[A[i]];}for(int i =1; i <= cnt2; i++){B1[i]= B[i];B[i]= ls[B[i]];}ans +=query_sum(l, mid, L, R);for(int i =1; i <= cnt1; i++){A[i]= A1[i];}for(int i =1; i <= cnt2; i++){B[i]= B1[i];}}if(R > mid){for(int i =1; i <= cnt1; i++){A1[i]= A[i];A[i]= rs[A[i]];}for(int i =1; i <= cnt2; i++){B1[i]= B[i];B[i]= rs[B[i]];}ans +=query_sum(mid +1, r, L, R);for(int i =1; i <= cnt1; i++){A[i]= A1[i];}for(int i =1; i <= cnt2; i++){B[i]= B1[i];}}return ans;}intget_sum(int l,int r,int L,int R){if(l > r || L > R){return0;}cnt1 = cnt2 =0;for(int i = l -1; i; i -=lowbit(i)){A[++cnt1]= root[i];}for(int i = r; i; i -=lowbit(i)){B[++cnt2]= root[i];}returnquery_sum(1, n, L, R);}intmain(){// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d %d",&n,&k);for(int i =1, x; i <= n; i++){scanf("%d",&x);l[x]= i;}for(int i =1, x; i <= n; i++){scanf("%d",&x);r[x]= i;}for(int i =1; i <= n; i++){a[i]=Res(l[i], r[i], i);}sort(a +1, a +1+ n);longlong ans =0;for(int i =1; i <= n; i++){ans +=get_sum(a[i].y +1, n, k + a[i].z +1, n);ans +=get_sum(a[i].y +1, n,1, a[i].z - k -1);for(int j = a[i].y; j <= n; j +=lowbit(j)){update(root[j],1, n, a[i].z,1);}}printf("%lld\n", ans);return0;}