#include<bits/stdc++.h>
using namespace std;
const int N = 2e6 + 9;
int n,m;
struct card{
int num,id,typ;
} a[N];
bool cmp(card c1,card c2){
return (c1.num ^ c2.num) ? c1.num < c2.num : c1.typ < c2.typ;
}
int vis[N];
int cnt;
int tot,bucket[N][2];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> n >> m;
for(int i = 1;i <= n;i++){
cin >> a[i].num;
a[i].id = i;
a[i].typ = 1;
}
for(int i = 1;i <= n;i++){
cin >> a[i + n].num;
a[i + n].id = i;
a[i + n].typ = 2;
}
n <<= 1;
sort(a + 1,a + n + 1,cmp);
int l = 1,r = 0;
int ans = INT_MAX;
while(l <= n){
while(r < n && cnt < (n >> 1)){
r++;
vis[a[r].id]++;
bucket[a[r].id][a[r].typ]++;
if(vis[a[r].id] == 1)
cnt++;
if(a[r].typ == 1 && bucket[a[r].id][2])
tot--;
if(a[r].typ == 2 && !bucket[a[r].id][1])
tot++;
}
if(cnt == (n >> 1) && tot <= m)
ans = min(ans,a[r].num - a[l].num);
vis[a[l].id]--;
if(!vis[a[l].id])
cnt--;
if(a[l].typ == 1 && bucket[a[l].id][2])
tot++;
if(a[l].typ == 2 && !bucket[a[l].id][1])
tot--;
bucket[a[l].id][a[l].typ]--;
l++;
}
cout << ans;
return 0;
}