#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#include "grader.h"
#define int long long
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N=getN(),K=getK(),M=4e8;
if(N>1000)return 1;
ordered_set<int>s;
auto calc=[&](int x)->int{
int c=0;
for(int i:s){
if(i<x){
c+=x-i;
}
if(i>x+K){
c+=i-(x+K);
}
}
return c;
};
for(int i=0;i<N;i++){
s.insert(Data());
int ans=1e18;
{
int l=0,h=i;
while(l<h){
int m=(l+h)/2;
if(m>=i-s.order_of_key(*s.find_by_order(m)+K)){
h=m;
}
else{
l=m+1;
}
}
ans=min(ans,calc(*s.find_by_order(h)));
if(h>0)ans=min(ans,calc(*s.find_by_order(h-1)));
}
{
int l=0,h=i;
while(l<h){
int m=(l+h)/2;
if(s.order_of_key(*s.find_by_order(m)-K)>=i-m){
h=m;
}
else{
l=m+1;
}
}
ans=min(ans,calc(*s.find_by_order(h)-K));
if(h>0)ans=min(ans,calc(*s.find_by_order(h-1)-K));
}
Solution(ans);
}
return 0;
}