#include <bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp> // Include for built in treap
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
const int MOD = 1e9 + 7;
#define ll long long int
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define eb emplace_back
#define in insert
#define ff first
#define ss second
#define vi vector<int>
#define vl vector<ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define si set<int>
#define mpii map<int, int>
#define prq priority_queue
#define sz(s) (int) s.size()
#define pf(a) printf("%d ",a)
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define si3(a,b,c) scanf("%d%d%d",&a,&b,&c)
//#define scan(v) for(int i = 0; i<n; i++){int x;cin>>x;v.pb(x);}
#define pi 2 * acos (0.0)
#define inf 1e9
#define co cout
#define en '\n'
#define re return
#define con continue
#define dbg cout<<"___________ok____________"<<endl;
#define yes() printf("YES\n");
#define no() puts("NO");
#define FOR(i, j, k) for(int i=(j); i<(k); i++)
#define REV(i, j, k) for(int i=(n-1); i>=k; i--)
#define all(v) v.begin(), v.end()
#define rnd(a, b) ((((rand()<<15)^rand())%((b)-(a) + 1))+(a))
#define FAST_IO ios_base::sync_with_stdio(false),cin.tie(NULL)
#define mem(x, y) memset(x, y, sizeof(x))
#define prn(v) for(auto x: v)cout<<x<<" ";
//typedef tree <int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pb_ds;
const double EPS = 1e-9;
const int MXX = 2e5 + 5;
ll a[2005], b[2005], c[2005];
int main()
{
FAST_IO;
ll n, m;cin>>n>>m;
ll mxp = -1;
for(ll i = 0; i<n; i++)cin>>a[i];
for(ll i = 0; i<n; i++)cin>>b[i], mxp=max(mxp, b[i]),c[i] = b[i];
sort(c, c + n);
for(ll i = 0; i<=1e5; i++)
{
vector<ll>v;
for(ll j = 0; j<n; j++)
{
v.pb((a[j] + i)%m);
}
sort(all(v));
// if(i == 1) prn(v);re 0;
bool f = 1;
for(ll j = 0; j<n; j++)
{
if(c[j]!=v[j])
{
f = 0;break;
}
}
if(f)return cout<<i<<endl, 0;
}
return 0;
}
ICAgICAgICAjaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KICAgICAgICB1c2luZyBuYW1lc3BhY2Ugc3RkOwogICAgICAgIC8vI2luY2x1ZGUgPGV4dC9wYl9kcy9hc3NvY19jb250YWluZXIuaHBwPiAvLyBJbmNsdWRlIGZvciBidWlsdCBpbiB0cmVhcAogICAgICAgIC8vI2luY2x1ZGUgPGV4dC9wYl9kcy90cmVlX3BvbGljeS5ocHA+CiAgICAgICAgLy91c2luZyBuYW1lc3BhY2UgX19nbnVfcGJkczsKICAgICAgICBjb25zdCBpbnQgTU9EID0gMWU5ICsgNzsKICAgICAgICAjZGVmaW5lIGxsIGxvbmcgbG9uZyBpbnQKICAgICAgICAjZGVmaW5lIHBpaSBwYWlyPGludCwgaW50PgogICAgICAgICNkZWZpbmUgcGxsIHBhaXI8bGwsIGxsPgogICAgICAgICNkZWZpbmUgcGIgcHVzaF9iYWNrCiAgICAgICAgI2RlZmluZSBlYiBlbXBsYWNlX2JhY2sKICAgICAgICAjZGVmaW5lIGluIGluc2VydAogICAgICAgICNkZWZpbmUgZmYgZmlyc3QKICAgICAgICAjZGVmaW5lIHNzIHNlY29uZAogICAgICAgICNkZWZpbmUgdmkgdmVjdG9yPGludD4KICAgICAgICAjZGVmaW5lIHZsIHZlY3RvcjxsbD4KICAgICAgICAjZGVmaW5lIHZwaWkgdmVjdG9yPHBpaT4KICAgICAgICAjZGVmaW5lIHZwbGwgdmVjdG9yPHBsbD4KICAgICAgICAjZGVmaW5lIHNpIHNldDxpbnQ+CiAgICAgICAgI2RlZmluZSBtcGlpIG1hcDxpbnQsIGludD4KICAgICAgICAjZGVmaW5lIHBycSBwcmlvcml0eV9xdWV1ZQogICAgICAgICNkZWZpbmUgc3oocykgKGludCkgcy5zaXplKCkKICAgICAgICAjZGVmaW5lIHBmKGEpIHByaW50ZigiJWQgIixhKQogICAgICAgICNkZWZpbmUgc2kxKGEpIHNjYW5mKCIlZCIsJmEpCiAgICAgICAgI2RlZmluZSBzaTIoYSxiKSBzY2FuZigiJWQlZCIsJmEsJmIpCiAgICAgICAgI2RlZmluZSBzaTMoYSxiLGMpIHNjYW5mKCIlZCVkJWQiLCZhLCZiLCZjKQogICAgICAgIC8vI2RlZmluZSBzY2FuKHYpIGZvcihpbnQgaSA9IDA7IGk8bjsgaSsrKXtpbnQgeDtjaW4+Png7di5wYih4KTt9CiAgICAgICAgI2RlZmluZSBwaSAyICogYWNvcyAoMC4wKQogICAgICAgICNkZWZpbmUgaW5mIDFlOQogICAgICAgICNkZWZpbmUgY28gY291dAogICAgICAgICNkZWZpbmUgZW4gJ1xuJwogICAgICAgICNkZWZpbmUgcmUgcmV0dXJuCiAgICAgICAgI2RlZmluZSBjb24gY29udGludWUKICAgICAgICAjZGVmaW5lIGRiZyBjb3V0PDwiX19fX19fX19fX19va19fX19fX19fX19fXyI8PGVuZGw7CiAgICAgICAgI2RlZmluZSB5ZXMoKSBwcmludGYoIllFU1xuIik7CiAgICAgICAgI2RlZmluZSBubygpIHB1dHMoIk5PIik7CiAgICAgICAgI2RlZmluZSBGT1IoaSwgaiwgaykgZm9yKGludCBpPShqKTsgaTwoayk7IGkrKykKICAgICAgICAjZGVmaW5lIFJFVihpLCBqLCBrKSBmb3IoaW50IGk9KG4tMSk7IGk+PWs7IGktLSkKICAgICAgICAjZGVmaW5lIGFsbCh2KSB2LmJlZ2luKCksIHYuZW5kKCkKICAgICAgICAjZGVmaW5lIHJuZChhLCBiKSAoKCgocmFuZCgpPDwxNSlecmFuZCgpKSUoKGIpLShhKSArIDEpKSsoYSkpCiAgICAgICAgI2RlZmluZSBGQVNUX0lPIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpLGNpbi50aWUoTlVMTCkKICAgICAgICAjZGVmaW5lIG1lbSh4LCB5KSBtZW1zZXQoeCwgeSwgc2l6ZW9mKHgpKQogICAgICAgICNkZWZpbmUgcHJuKHYpIGZvcihhdXRvIHg6IHYpY291dDw8eDw8IiAiOwogICAgICAgIC8vdHlwZWRlZiB0cmVlIDxpbnQsIG51bGxfdHlwZSwgbGVzczxpbnQ+LCByYl90cmVlX3RhZywgdHJlZV9vcmRlcl9zdGF0aXN0aWNzX25vZGVfdXBkYXRlPiBwYl9kczsKICAgICAgICBjb25zdCBkb3VibGUgRVBTID0gMWUtOTsKICAgICAgICBjb25zdCBpbnQgTVhYID0gMmU1ICsgNTsKICAgICAgICAgCiAgICAgICAgbGwgYVsyMDA1XSwgYlsyMDA1XSwgY1syMDA1XTsKICAgICAgICBpbnQgbWFpbigpCiAgICAgICAgewogICAgICAgICAKICAgICAgICAgICAgRkFTVF9JTzsKICAgICAgICAgICAgbGwgbiwgbTtjaW4+Pm4+Pm07CiAgICAgICAgICAgIGxsIG14cCA9IC0xOwogICAgICAgICAgICBmb3IobGwgaSA9IDA7IGk8bjsgaSsrKWNpbj4+YVtpXTsKICAgICAgICAgICAgZm9yKGxsIGkgPSAwOyBpPG47IGkrKyljaW4+PmJbaV0sIG14cD1tYXgobXhwLCBiW2ldKSxjW2ldID0gYltpXTsKICAgICAgICAgICAgc29ydChjLCBjICsgbik7CiAgICAgICAgICAgIGZvcihsbCBpID0gMDsgaTw9MWU1OyBpKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHZlY3RvcjxsbD52OwogICAgICAgICAgICAgICAgZm9yKGxsIGogPSAwOyBqPG47IGorKykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICB2LnBiKChhW2pdICsgaSklbSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBzb3J0KGFsbCh2KSk7CiAgICAgICAgICAgICAgLy8gaWYoaSA9PSAxKSBwcm4odik7cmUgMDsKICAgICAgICAgICAgICAgIGJvb2wgZiA9IDE7CiAgICAgICAgICAgICAgICBmb3IobGwgaiA9IDA7IGo8bjsgaisrKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGlmKGNbal0hPXZbal0pCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBmID0gMDticmVhazsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZihmKXJldHVybiBjb3V0PDxpPDxlbmRsLCAwOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiAwOwogICAgICAgIH0=