#include <bits/stdc++.h>
using namespace std;
#define maxn 100010
#define pii pair<int,int>
#define mp make_pair
#define F first
#define S second
int a[maxn], b[maxn], posa[maxn], posb[maxn];
int p[maxn], q[maxn];
bool pt;
pii u[maxn];
int dp[maxn], prev[maxn];
struct node{
int lft;
int rgt;
int val;
node(){
lft= 0;
rgt= 0;
val= 0;
}
};
#define SN 20000007
node segtree[SN];
int curr = 0;
void upd (int &n, int L, int R, int ind, int res){
if (L>R or L>ind or ind>R) return;
if(!n){
n = ++curr;
}
++segtree[n].val;
if(L < R){
int mid= (L+R)/2;
if (ind<=mid){
upd(segtree[n].lft, L, mid, ind, res);
}
else{
upd (segtree[n].rgt, mid+1, R, ind, res);
}
}
}
int query (int n, int L, int R, int S, int E){
if (L>R or L>E or S>R or !n) return 0;
if (L>=S and R<=E) {
return segtree[n].val;
}
int mid= (L+R)/2;
return query(segtree[n].lft, L, mid, S, E)+ query(segtree[n].rgt, mid+1, R, S, E);
}
int bit[maxn];
int N, n;
void treeupd (int x, int y){
while (x<N){
upd(bit[x], 1, n, y, 1);
x+=x&(-x);
}
}
int treequery (int x, int y1, int y2){
if (x==0 or y1==0 or y2==0 or y1>y2) return 0;
int ans= 0;
while (x){
ans+=query(bit[x], 1, n, y1, y2);
x-=x&(-x);
}
return ans;
}
int check (int x){
if (x<=0 or x>n) return 0;
return 1;
}
int ask (int x1, int x2, int y1, int y2){
if (x1>x2 or y1>y2 or !check(x1) or !check(x2) or !check(y1) or !check(y2)) return 0;
return treequery(x2, y1, y2) - treequery(x1-1, y1, y2);
}
int ans[maxn];
int main(){
freopen ("friendcross.in", "r", stdin);
freopen ("friendcross.out", "w", stdout);
int k;
cin>>n>>k;
for (int i=1; i<=n; i++) {
cin>>a[i];
posa[a[i]]= i;
}
for (int i=1; i<=n; i++) {
cin>>b[i];
posb[b[i]]= i;
}
N= n+5;
for (int i=1; i<=n; i++){
if (i+k+1>n) continue;
int t= i+k+1;
treeupd(posa[i], posb[i]);
ans[i+k+1]= ask(1, posa[t], posb[t], n) + ask(posa[t], n, 1, posb[t]) - ask(posa[t], posa[t], posb[t], posb[t]);
}
long long int anss= 0LL;
for (int i=1; i<=n; i++) anss+=ans[i];
cout<<anss<<endl;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbWF4biAxMDAwMTAKI2RlZmluZSBwaWkgcGFpcjxpbnQsaW50PgojZGVmaW5lIG1wIG1ha2VfcGFpcgojZGVmaW5lIEYgZmlyc3QKI2RlZmluZSBTIHNlY29uZAppbnQgYVttYXhuXSwgYlttYXhuXSwgcG9zYVttYXhuXSwgcG9zYlttYXhuXTsKCmludCBwW21heG5dLCBxW21heG5dOwpib29sIHB0OwpwaWkgdVttYXhuXTsKaW50IGRwW21heG5dLCBwcmV2W21heG5dOwpzdHJ1Y3Qgbm9kZXsKCWludCBsZnQ7CglpbnQgcmd0OwoJaW50IHZhbDsgCglub2RlKCl7CgkJbGZ0PSAwOwoJCXJndD0gMDsKCQl2YWw9IDA7Cgl9Cn07CiNkZWZpbmUgU04gMjAwMDAwMDcKbm9kZSBzZWd0cmVlW1NOXTsKCmludCBjdXJyID0gMDsKdm9pZCB1cGQgKGludCAmbiwgaW50IEwsIGludCBSLCBpbnQgaW5kLCBpbnQgcmVzKXsKCWlmIChMPlIgb3IgTD5pbmQgb3IgaW5kPlIpIHJldHVybjsKCWlmKCFuKXsKCQluID0gKytjdXJyOwoJfQoJKytzZWd0cmVlW25dLnZhbDsKCWlmKEwgPCBSKXsKCQlpbnQgbWlkPSAoTCtSKS8yOwoJCWlmIChpbmQ8PW1pZCl7CgkJCXVwZChzZWd0cmVlW25dLmxmdCwgTCwgbWlkLCBpbmQsIHJlcyk7CgkJfQoJCWVsc2V7CgkJCXVwZCAoc2VndHJlZVtuXS5yZ3QsIG1pZCsxLCBSLCBpbmQsIHJlcyk7CgkJfQoJfQp9CgppbnQgcXVlcnkgKGludCBuLCBpbnQgTCwg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