#include <bits/stdc++.h>

#define ll long long
#define f(i,k,n) for(i=k;i<n;i++)
#define mi map<ll,ll>::iterator

using namespace std;

int main(){
    ll n,m,i,j;
    cin>>n>>m;
    ll a[n],b[n];
    f(i,0,n){
        cin>>a[i];
    }

    f(i,0,n){
        cin>>b[i];
    }

    map<ll,ll> m1;

    map<ll,ll> m2;

    f(i,0,n){
        mi it = m1.find(a[i]);
        if(it == m1.end()){
            m1[a[i]]=1;
        }
        else{
            it->second++;
        }
    }
    f(i,0,n){
        mi it = m2.find(b[i]);
        if(it == m2.end()){
            m2[b[i]]=1;
        }
        else{
            it->second++;
        }
    }

    mi itr = m1.begin();
    ll index=itr->first;

    //cout<<itr->second<<endl;
    for(mi it = m2.begin();it!=m2.end();it++){
            //cout<<it1->first<<ends<<it2->first<<ends<<it1->second<<ends<<it2->second<<endl;
            map<ll,ll> hash;
            if((it->second == itr->second)){
                index=(m+(it->first-itr->first))%m;
               for(mi it1 = m1.begin();it1!=m1.end();it1++)
                   hash[(it1->first+index)%m]=it1->second;
                   int flag=0;
                for(mi it1 = hash.begin(),it2 = m2.begin();it1!=hash.end(),it2!=m2.end();it1++,it2++){
                    if(it1->first!=it2->first || it1->second!=it2->second){
                        flag=1;
                        break;
                    }
                }
                if(flag==0)
                    break;
            }

    }

    cout<<index<<endl;
    return 0;

}


#include <bits/stdc++.h>

#define ll long long
#define f(i,k,n) for(i=k;i<n;i++)
#define mi map<ll,ll>::iterator

using namespace std;

int main(){
    ll n,m,i,j;
    cin>>n>>m;
    ll a[n],b[n];
    f(i,0,n){
        cin>>a[i];
    }

    f(i,0,n){
        cin>>b[i];
    }

    map<ll,ll> m1;

    map<ll,ll> m2;

    f(i,0,n){
        mi it = m1.find(a[i]);
        if(it == m1.end()){
            m1[a[i]]=1;
        }
        else{
            it->second++;
        }
    }
    f(i,0,n){
        mi it = m2.find(b[i]);
        if(it == m2.end()){
            m2[b[i]]=1;
        }
        else{
            it->second++;
        }
    }

//    for(mi it = m2.begin();it!=m2.end();it++){
//            cout<<it->first<<ends<<it->second<<endl;
//    }
    getchar();
    ll x=0;

    while(1){
        //cout<<x<<endl;
        map<ll,ll> hash;

        for(mi it = m1.begin();it!=m1.end();it++){
            hash[((it->first)+x)%m]=it->second;
        }
//        for(mi it = hash.begin();it!=hash.end();it++){
//            cout<<it->first<<ends<<it->second<<endl;
//        }
//        for(mi it = m2.begin();it!=m2.end();it++){
//            cout<<it->first<<ends<<it->second<<endl;
//        }
        int flag=0;
        for(mi it1 = hash.begin(),it2 = m2.begin();it1!=hash.end(),it2!=m2.end();it1++,it2++){
            //cout<<it1->first<<ends<<it1->second<<ends<<it2->first<<ends<<it2->second<<endl;
            if(it1->first != it2->first || it1->second != it2->second){
                //cout<<"inside"<<endl;
                //cout<<it1->first<<ends<<it1->second<<ends<<it2->first<<ends<<it2->second<<endl;
                flag=1;
                break;
            }

        }
        if(flag==0)
            break;
        x++;
    }

    cout<<x<<endl;
    return 0;

}


#include <bits/stdc++.h>

#define ll long long
#define f(i,k,n) for(i=k;i<n;i++)
#define mi map<ll,ll>::iterator

using namespace std;

int main(){
    ll n,m,i,j;
    cin>>n>>m;
    ll a[n],b[n];
    f(i,0,n){
        cin>>a[i];
    }

    f(i,0,n){
        cin>>b[i];
    }

    map<ll,ll> m1;

    map<ll,ll> m2;

    f(i,0,n){
        mi it = m1.find(a[i]);
        if(it == m1.end()){
            m1[a[i]]=1;
        }
        else{
            it->second++;
        }
    }
    f(i,0,n){
        mi it = m2.find(b[i]);
        if(it == m2.end()){
            m2[b[i]]=1;
        }
        else{
            it->second++;
        }
    }

    ll x=0,index=0;
    mi itr = m1.begin();
    //cout<<itr->second<<endl;
    for(mi it = m2.begin();it!=m2.end();it++){
            //cout<<it1->first<<ends<<it2->first<<ends<<it1->second<<ends<<it2->second<<endl;
            if((it->second == itr->second)){
                index=it->first;
                break;
            }

    }

    cout<<(m+(index-itr->first))%m<<endl;
    return 0;

}
    //cout<<index1<<ends<<index2<<endl;

    //
//    while(1){
//
//        map<ll,ll> hash;
//
//        for(mi it = m1.begin();it!=m1.end();it++){
//            hash[((it->first)+x)%m]=it->second;
//        }
//
//        int flag=0;
//        for(mi it1 = hash.begin(),it2 = m2.begin();it1!=hash.end(),it2!=m2.end();it1++,it2++){
//
//            if(it1->first != it2->first || it1->second != it2->second){
//                flag=1;
//                break;
//            }
//
//        }
//        if(flag==0)
//            break;
//        x++;
//    }


#include <bits/stdc++.h>

#define ll long long
#define f(i,k,n) for(i=k;i<n;i++)
#define mi map<ll,ll>::iterator

using namespace std;

int main(){
    ll n,m,i,j;
    cin>>n>>m;
    ll a[n],b[n];
    f(i,0,n){
        cin>>a[i];
    }

    f(i,0,n){
        cin>>b[i];
    }

    map<ll,ll> m1;

    map<ll,ll> m2;

    f(i,0,n){
        mi it = m1.find(a[i]);
        if(it == m1.end()){
            m1[a[i]]=1;
        }
        else{
            it->second++;
        }
    }
    f(i,0,n){
        mi it = m2.find(b[i]);
        if(it == m2.end()){
            m2[b[i]]=1;
        }
        else{
            it->second++;
        }
    }

    mi itr = m1.begin();
    ll index=itr->first,min=-1;

    //cout<<itr->second<<endl;
    for(mi it = m2.begin();it!=m2.end();it++){
            //cout<<it1->first<<ends<<it2->first<<ends<<it1->second<<ends<<it2->second<<endl;
            map<ll,ll> hash;
            if((it->second == itr->second)){
                index=(m+(it->first-itr->first))%m;
               for(mi it1 = m1.begin();it1!=m1.end();it1++)
                   hash[(it1->first+index)%m]=it1->second;
                   int flag=0;
                for(mi it1 = hash.begin(),it2 = m2.begin();it1!=hash.end(),it2!=m2.end();it1++,it2++){
                    if(it1->first!=it2->first || it1->second!=it2->second){
                        flag=1;
                        break;
                    }
                }
                if(flag==0){
                    if(min==-1 || index<min)
                        min=index;
                }
            }

    }

    cout<<min<<endl;
    return 0;

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[2001],b[2001];
void hzh()
{
	ll n,m,i,j;
	scanf("%lld%lld",&n,&m);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	for(i=0;i<n;i++)
		scanf("%lld",&b[i]);
	sort(a,a+n);
	sort(b,b+n);
	for(i=1;;i++)
	{
		for(j=0;j<n;j++)
		{
			if(a[j]!=b[j])
				break;
			if(j==n-1)
				{
					printf("%lld",i-1);
					return;
				}
		}
		for(j=0;j<n;j++)
			a[j]=(a[j]+i)%m;
		sort(a,a+n);
	}
}
int main()
{
	/*
	int t;
	scanf("%d",&t);
	while(t--)
	*/
    	hzh();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[2001],b[2001],c[2001];
void hzh()
{
	ll n,m,i,j,x,minx=1e9+1;
	scanf("%lld%lld",&n,&m);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	for(i=0;i<n;i++)
		scanf("%lld",&b[i]);
	sort(b,b+n);
	for(i=0;i<n;i++)
	{
		x=(a[0]-b[i]+m)%m;
		for(j=0;j<n;j++)
			c[j]=(a[j]+x)%m;
		sort(c,c+n);
		for(j=0;j<n;j++)
		{
			if(c[j]!=b[j])
				break;
			if(j==n-1)
				{
					minx=min(x,minx);
				}
		}
	}
	printf("%d",minx);
	return;
}
int main()
{
	/*
	int t;
	scanf("%d",&t);
	while(t--)
	*/
    	hzh();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[2001],b[2001];
void hzh()
{
	int i,j;
	ll n,m;
	scanf("%lld%lld",&n,&m);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	for(i=0;i<n;i++)
		scanf("%lld",&b[i]);
	sort(a,a+n);
	sort(b,b+n);
	for(i=1;;i++)
	{
		for(j=0;j<n;j++)
		{
			if(a[j]!=b[j])
				break;
			if(j==n-1)
				{
					printf("%d",i-1);
					return;
				}
		}
		for(j=0;j<n;j++)
			a[j]=(a[j]+i)%m;
		sort(a,a+n);
	}
}
int main()
{
	/*
	int t;
	scanf("%d",&t);
	while(t--)
	*/
    	hzh();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[2001],b[2001],c[2001];
void hzh()
{
int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++){
        scanf("%d",&b[i]);
    }
    sort(b+1,b+n+1);
    int ans=m;
    for(int i=1;i<=n;i++){
        int tmp=(b[i]-a[1]+m)%m;
        for(int j=1;j<=n;j++){
            c[j]=(a[j]+tmp)%m;
        }
        sort(c+1,c+n+1);
        for(int j=1;j<=n;j++){
            if(c[j]!=b[j]){
                break;
            }
            if(j==n){
                ans=min(ans,tmp);
            }
        }
    }
    printf("%d\n",ans);
}
int main()
{
	/*
	ll t;
	scanf("%lld",&t);
	while(t--)
	*/
    	hzh();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[2001],b[2001];
void hzh()
{
	int i,j,c,flag=1;
	ll n,m;
	scanf("%lld%lld",&n,&m);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	for(i=0;i<n;i++)
		scanf("%lld",&b[i]);
	sort(a,a+n);
	sort(b,b+n);
	c=b[0]-a[0];
	for(i=0;i<n;i++)
	{
		if(b[i]-a[i]!=c)
		{
			flag=0;
			break;
		}
	}
	if(flag&&c>=0)
	{
		cout<<c;
		return;
	}
	flag=1;
	for(i=0;i<n;i++)
	{
		i=0;
		c=b[i];
		while(c==b[i])
		{
			b[i]+=m;
			i++;
		}
		sort(b,b+n);
		c=b[0]-a[0];
		flag=1;
		for(j=1;j<n;j++)
		{
			if(b[j]-a[j]!=c)
			{
				flag=0;
				break;
			}
		}
		if(flag==1)
		break;
	}
	cout<<c;
}

int main()
{
	/*
	int t;
	scanf("%d",&t);
	while(t--)
	*/
    	hzh();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[2001],b[2001];
void hzh()
{
	long long int i,j;
	ll n,m;
	scanf("%lld%lld",&n,&m);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	for(i=0;i<n;i++)
		scanf("%lld",&b[i]);
	sort(a,a+n);
	sort(b,b+n);
	for(i=1;;i++)
	{
		for(j=0;;j++)
		{
			if(a[j]!=b[j])
				break;
			else
				{
					printf("%lld",i-1);
					return;
				}
		}
		for(j=0;j<n;j++)
			a[j]=(a[j]+i)%m;
		sort(a,a+n);
	}
}
int main()
{
	/*
	int t;
	scanf("%d",&t);
	while(t--)
	*/
    	hzh();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[2003],b[2003],c[2003];
void hzh()
{
	ll n,m,i,j,x,minx;
	scanf("%lld%lld",&n,&m);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	for(i=0;i<n;i++)
		scanf("%lld",&b[i]);
	sort(b,b+n);
	minx=m;
	for(i=0;i<n;i++)
	{
		x=(a[0]-b[i]+m)%m;
		for(j=0;j<n;j++)
			c[j]=(a[j]+x)%m;
		sort(c,c+n);
		for(j=0;j<n;j++)
		{
			if(c[j]!=b[j])
				break;
			if(j==n-1)
					minx=min(x,minx);
		}
	}
	printf("%lld",minx);
	return;
}
int main()
{
	/*
	ll t;
	scanf("%lld",&t);
	while(t--)
	*/
    	hzh();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[20003],b[20003],c[20003];
void hzh()
{
	ll n,m,i,j,x,minx;
	scanf("%lld%lld",&n,&m);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	for(i=0;i<n;i++)
		scanf("%lld",&b[i]);
	sort(b,b+n);
	minx=m;
	for(i=0;i<n;i++)
	{
		x=(a[0]-b[i]+m)%m;
		for(j=0;j<n;j++)
			c[j]=(a[j]+x)%m;
		sort(c,c+n);
		for(j=0;j<n;j++)
		{
			if(c[j]!=b[j])
				break;
			if(j==n-1)
					minx=min(x,minx);
		}
	}
	printf("%lld",minx);
	return;
}
int main()
{
	/*
	ll t;
	scanf("%lld",&t);
	while(t--)
	*/
    	hzh();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[2001],b[2001],c[2001];
void hzh()
{
	ll n,m,i,j,x,minx=-1;
	scanf("%lld%lld",&n,&m);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	for(i=0;i<n;i++)
		scanf("%lld",&b[i]);
	sort(b,b+n);
	for(i=0;i<n;i++)
	{
		x=(a[0]-b[i]+m)%m;
		for(j=0;j<n;j++)
			c[j]=(a[j]+x)%m;
		sort(c,c+n);
		for(j=0;j<n;j++)
		{
			if(c[j]!=b[j])
				break;
			if(j==n-1)
				{
					minx=min(x,minx);
				}
		}
	}
	printf("%d",minx);
	return;
}
int main()
{
	/*
	int t;
	scanf("%d",&t);
	while(t--)
	*/
    	hzh();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[2001],b[2001];
void hzh()
{
	int i,j;
	ll n,m;
	scanf("%lld%lld",&n,&m);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	for(i=0;i<n;i++)
		scanf("%lld",&b[i]);
	sort(a,a+n);
	sort(b,b+n);
	for(i=1;;i++)
	{
		for(j=0;j<n;j++)
		{
			if(a[j]!=b[j])
				break;
			if(j==n-1)
				{
					printf("%lld",i-1);
					return;
				}
		}
		for(j=0;j<n;j++)
			a[j]=(a[j]+i)%m;
		sort(a,a+n);
	}
}
int main()
{
	/*
	int t;
	scanf("%d",&t);
	while(t--)
	*/
    	hzh();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[2001],b[2001];
void hzh()
{
	int i,j;
	ll n,m;
	scanf("%lld%lld",&n,&m);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	for(i=0;i<n;i++)
		scanf("%lld",&b[i]);
	sort(a,a+n);
	sort(b,b+n);
	for(i=1;;i++)
	{
		for(j=0;j<n;j++)
		{
			if(a[j]!=b[j])
				break;
			if(j==n-1)
				{
					printf("%d",i-1);
					return;
				}
		}
		for(j=0;j<n;j++)
			a[j]=(a[j]+i)%m;
		sort(a,a+n);
	}
}
int main()
{
	/*
	int t;
	scanf("%d",&t);
	while(t--)
	*/
    	hzh();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[2003],b[2003],c[2003];
void hzh()
{
	ll n,m,i,j,x,minx;
	scanf("%lld%lld",&n,&m);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	for(i=0;i<n;i++)
		scanf("%lld",&b[i]);
	sort(b,b+n);
	minx=m;
	for(i=0;i<n;i++)
	{
		x=(b[i]-a[0]+m)%m;
		for(j=0;j<n;j++)
			c[j]=(a[j]+x)%m;
		sort(c,c+n);
		for(j=0;j<n;j++)
		{
			if(c[j]!=b[j])
				break;
			if(j==n-1)
					minx=min(x,minx);
		}
	}
	printf("%lld",minx);
}
int main()
{
	/*
	ll t;
	scanf("%lld",&t);
	while(t--)
	*/
    	hzh();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[2001],b[2001],c[2001];
void hzh()
{
	ll n,m,i,j,x;
	scanf("%lld%lld",&n,&m);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	for(i=0;i<n;i++)
		scanf("%lld",&b[i]);
	sort(b,b+n);
	for(i=0;i<n;i++)
	{
		x=(a[0]-b[i]+m)%m;
		for(j=0;j<n;j++)
			c[j]=(a[j]+x)%m;
		sort(c,c+n);
		for(j=0;j<n;j++)
		{
			if(c[j]!=b[j])
				break;
			if(j==n-1)
				{
					printf("%lld",x);
					return;
				}
		}
	}
}
int main()
{
	/*
	int t;
	scanf("%d",&t);
	while(t--)
	*/
    	hzh();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[2001],b[2001],c[2001];
void hzh()
{
	ll n,m,i,j,x,minx;
	scanf("%lld%lld",&n,&m);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	for(i=0;i<n;i++)
		scanf("%lld",&b[i]);
	sort(b,b+n);
	minx=m;
	for(i=0;i<n;i++)
	{
		x=(a[0]-b[i]+m)%m;
		for(j=0;j<n;j++)
			c[j]=(a[j]+x)%m;
		sort(c,c+n);
		for(j=0;j<n;j++)
		{
			if(c[j]!=b[j])
				break;
			if(j==n-1)
					minx=min(x,minx);
		}
	}
	printf("%lld",minx);
	return;
}
int main()
{
	/*
	ll t;
	scanf("%lld",&t);
	while(t--)
	*/
    	hzh();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

set <int> s;
int a[2005];
int b[2005];
int aa[2005];
int n,m;

  bool per(int x)
  {
    for(int i = 0; i < n; i++)
       a[i] = ( a[i] + x ) % m;

    sort(a,a+n);

    for(int i = 0; i < n; i++)
       if (a[i] != b[i]) {
          for(int i = 0; i < n; i++)
             a[i] = aa[i];
             return false;
       }

     return true;

  }
int main()
{

   cin >> n >> m;

     for(int i = 0; i < n; i++)
        {
          cin >> a[i];
          aa[i] = a[i];
        }

     for(int i = 0; i < n; i++)
        cin >> b[i];

        sort(b,b+n);

         for(int i = 0; i < n; i++)
            if (b[0] >= a[i]) s.insert(b[0] - a[i]);
                        else s.insert(b[0] + m - a[i]);

          //  for(auto now : s)
          //     cout << now << " ";
           //    cout << "\n";

         for(auto now : s)
            if (per(now)) {
               cout << now;
               break;
            }
}


#include <bits/stdc++.h>
#define f(ch,l,r) for(int ch = l; ch < r; ch++)

using namespace std;

int main()
{

       int f = 0;
       int n,k;
       cin >> n >> k;

       string s;
       string d;

       cin >> s;

         cout << n << "\n";

        f(i,0,n)
          d += s[i%k];

          if (d >= s) cout << d;
          else {
               while(s[k-1] == '9')
                    {
                      s[k-1] = '0';
                      k--;
                    }
               s[k-1]++;

           f(i,0,n)
              cout << s[i%k];
          }

}


#include <bits/stdc++.h>
#include <stdio.h>
#define inf 10000000000
#define ll long long

using namespace std;
typedef pair <int,int> pii;

vector <long long> pr;

ll a[2005],b[2005],n,mod,ans,temp;
ll aa[2005];

map <ll,ll> s1,s2;
       bool per(ll x, ll y, ll temp)
       {
           for(int i = 0; i < n; i++)
              a[i] = (a[i] + temp) % mod;

            sort(a,a+n);

           for(int i = 0; i < n; i++)
           if (a[i] != b[i]) {
              for(int j = 0; j < n; j++)
                  a[j] = aa[j];
              return false;
           }
           return true;

       }
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(0);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);

  cin >> n >> mod;

   for(int i = 0; i < n; i++)
      {
          cin >> a[i];
          aa[i] = a[i];
          s1[a[i]]++;
      }
   for(int i = 0; i < n; i++)
      {
          cin >> b[i];
          s2[b[i]]++;
      }
      sort(b,b+n);
   ans = inf;
    for(int i = 0; i < n; i++)
    for(int j = 0; j < n; j++)
    if (s1[a[i]] == s2[b[j]] ) {
        if (a[i] <= b[j]) temp = b[j] - a[i];
                     else   temp = mod + b[j] - a[i];

        if (per(a[i],b[j],temp)) ans = min(ans,temp);
    }

    if (ans == inf) cout << 0;
    else            cout << ans;

}


#include <bits/stdc++.h>
#include <stdio.h>
#define inf 10000000000
#define ll long long

using namespace std;
typedef pair <int,int> pii;

vector <long long> pr;

ll a[2005],b[2005];
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(0);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);

  ll n, mod,ans = 0;
  cin >> n >> mod;

   for(int i = 0; i < n; i++)
      cin >> a[i];
   for(int i = 0; i < n; i++)
      cin >> b[i];

      sort(a,a+n);
      sort(b,b+n);

       int it = 0;
       for(int i = 0; i < n; i++)
           if (abs(a[i] - b[i]) > ans) {
              ans = abs(a[i]-b[i]);
              it = i;
           }

        cout << ans;
}


#include <bits/stdc++.h>
#include <stdio.h>
#define inf 10000000000
#define ll long long

using namespace std;
typedef pair <int,int> pii;

vector <long long> pr;

ll a[2005],b[2005],n,mod,ans,temp;
ll aa[2005];

map <ll,ll> s1,s2;
       bool per(ll x, ll y, ll temp)
       {
           for(int i = 0; i < n; i++)
              a[i] = (a[i] + temp) % mod;

            sort(a,a+n);

           for(int i = 0; i < n; i++)
           if (a[i] != b[i]) {
              for(int j = 0; j < n; i++)
                  a[i] = aa[i];
              return false;
           }
           return true;

       }
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(0);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);

  cin >> n >> mod;

   for(int i = 0; i < n; i++)
      {
          cin >> a[i];
          aa[i] = a[i];
          s1[a[i]]++;
      }
   for(int i = 0; i < n; i++)
      {
          cin >> b[i];
          s2[b[i]]++;
      }
   ans = inf;
    for(int i = 0; i < n; i++)
    for(int j = 0; j < n; j++)
    if (s1[a[i]] == s2[b[j]] ) {
        if (s1[a[i]] <= s2[b[j]]) temp = b[j] - a[i];
                     else   temp = mod + b[j] - a[i];

        if (per(a[i],b[j],temp)) ans = min(ans,temp);
    }

     cout << ans;

}


#include <bits/stdc++.h>
#include <stdio.h>
#define inf 10000000000
#define ll long long

using namespace std;
typedef pair <int,int> pii;

vector <long long> pr;

ll a[2005],b[2005],n,mod,ans;

multiset <ll> s1,s2;
       bool per(ll x)
       {
           s1.clear();
           for(int i = 0; i < n; i++)
             {
                 a[i] = (a[i] + x) % mod;
                 s1.insert(a[i]);
             }

            if (s1 == s2) return true;
                     else return false;

       }
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(0);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);

  cin >> n >> mod;

   for(int i = 0; i < n; i++)
      cin >> a[i];
   for(int i = 0; i < n; i++)
      {
          cin >> b[i];
          s2.insert(b[i]);
      }

      sort(a,a+n);
      sort(b,b+n);

    for(int i = 0; i <= mod; i++)
    if (per(i)) {
        cout << i ;
        break;
    }

}


#include <bits/stdc++.h>
#include <stdio.h>
#define inf 10000000000
#define ll long long

using namespace std;
typedef pair <int,int> pii;

vector <long long> pr;

ll a[2005],b[2005];
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(0);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);

  ll n, mod,ans = 0;
  cin >> n >> mod;

   for(int i = 0; i < n; i++)
      cin >> a[i];
   for(int i = 0; i < n; i++)
      cin >> b[i];

      sort(a,a+n);
      sort(b,b+n);

       int it = 0;
       for(int i = 0; i < n; i++)
           if (abs(a[i] - b[i]) > ans) {
              ans = abs(a[i]-b[i]);
              it = i;
           }
       if (a[it] <= b[it]) cout << b[it] - a[it];
                     else cout << mod + b[it] - a[it];

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdb;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<string> vs;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdb> vpd;

#define mp make_pair
#define ff first
#define ss second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) (x).rbegin(), (x).rend()
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define vv <vector<vector> >
#define mp make_pair
#define str(a, b) substr((a), (b))
#define rr return
#define r0 return 0
#define mi map<int>
#define ms map<string>
#define ml map<ll>
#define all_sum(a,b) accumulate((a),(b),0)
#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
void adskiy_razgon(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1};
ll nod(ll a, ll b) {
    if(b > a) {
        swap(a, b);
    }
    while(b > 0) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll nok(ll a,ll b) {
    return a * b / nod(a, b);
}
void sp(ll a,double b) {
    cout << fixed << setprecision(a) << b;
}
ll binpow (ll a, ll n) {
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 1) {
        return binpow (a, n - 1) * a;
    }
    else {
        ll b = binpow (a, n / 2);
        return b * b;
    }
}

template<class A> void read(vector<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d=stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
    read(t...);
}
template<class A> void read(vector<A>& x) {
    EACH(a, x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    EACH(a, x)
        read(a);
}
string to_string(char c) {
    return string(1, c);
}
string to_string(bool b) {
    return b?"true":"false";
}
string to_string(const char* s) {
    return string(s);
}
string to_string(string s) {
    return s;
}
string to_string(vector<bool> v) {
    string res;
    FOR(sz(v))
        res+=char('0'+v[i]);
    return res;
}

template<size_t S> string to_string(bitset<S> b) {
    string res;
    FOR(S)
        res+=char('0'+b[i]);
    return res;
}
template<class T> string to_string(T v) {
    bool f=1;
    string res;
    EACH(x, v) {
        if(!f)
            res+=' ';
        f=0;
        res+=to_string(x);
    }
    return res;
}
template<class A> void write(A x) {
    cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) {
    write(h);
    write(t...);
}
void print() {
     write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) {
    write(h);
    if(sizeof...(t))
        write(' ');
    print(t...);
}

void yes(bool ok) {
    print((ok ? "YES" : "Yes"));
}
void no(bool ok) {
    print((ok ? "NO" : "No"));
}
const int MOD = 1e9+7; // 888244353;
const int N = 2e5+5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int M = 1005;
map<ll, ll> m;
ll mod, n;
bool check(ll x, map<ll,ll> y, vl a) {
    vl c = a;
    for(int i = 0; i < n; ++i) {
        c[i] += x;
        c[i] %= mod;
        if(y[c[i]]) y[c[i]]--;
        else return 0;
    }
    return 1;
}
void solve() {
    read(n, mod);
    vl a(n), b(n);
    for(int i = 0; i < n; ++i) read(a[i]);
    for(int i = 0; i < n; ++i) {
        read(b[i]);
        m[b[i]]++;
    }
    sort(all(b));
    sort(all(a));
    for(int i = 0; i < n; ++i) {
        if(check((b[i] - a[i] + mod) % mod, m, a)) {
            print((b[i] - a[i] + mod) % mod);
            rr;
        }
    }
}
int main(){
    adskiy_razgon();
    ll t = 1;
    //read(t);
    FOR(t) {
        //write("Case ", i, ": ");
        solve();
    }
    r0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdb;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<string> vs;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdb> vpd;

#define mp make_pair
#define ff first
#define ss second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) (x).rbegin(), (x).rend()
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define vv <vector<vector> >
#define mp make_pair
#define str(a, b) substr((a), (b))
#define rr return
#define r0 return 0
#define mi map<int>
#define ms map<string>
#define ml map<ll>
#define all_sum(a,b) accumulate((a),(b),0)
#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
void adskiy_razgon(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1};
ll nod(ll a, ll b) {
    if(b > a) {
        swap(a, b);
    }
    while(b > 0) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll nok(ll a,ll b) {
    return a * b / nod(a, b);
}
void sp(ll a,double b) {
    cout << fixed << setprecision(a) << b;
}
ll binpow (ll a, ll n) {
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 1) {
        return binpow (a, n - 1) * a;
    }
    else {
        ll b = binpow (a, n / 2);
        return b * b;
    }
}

template<class A> void read(vector<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d=stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
    read(t...);
}
template<class A> void read(vector<A>& x) {
    EACH(a, x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    EACH(a, x)
        read(a);
}
string to_string(char c) {
    return string(1, c);
}
string to_string(bool b) {
    return b?"true":"false";
}
string to_string(const char* s) {
    return string(s);
}
string to_string(string s) {
    return s;
}
string to_string(vector<bool> v) {
    string res;
    FOR(sz(v))
        res+=char('0'+v[i]);
    return res;
}

template<size_t S> string to_string(bitset<S> b) {
    string res;
    FOR(S)
        res+=char('0'+b[i]);
    return res;
}
template<class T> string to_string(T v) {
    bool f=1;
    string res;
    EACH(x, v) {
        if(!f)
            res+=' ';
        f=0;
        res+=to_string(x);
    }
    return res;
}
template<class A> void write(A x) {
    cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) {
    write(h);
    write(t...);
}
void print() {
     write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) {
    write(h);
    if(sizeof...(t))
        write(' ');
    print(t...);
}

void yes(bool ok) {
    print((ok ? "YES" : "Yes"));
}
void no(bool ok) {
    print((ok ? "NO" : "No"));
}
const int MOD = 1e9+7; // 888244353;
const int N = 2e5+5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int M = 1005;
vi a, b, c;
int n, mod;
bool check(int x) {
    c = a;
    for(int i = 0; i < sz(c); ++i) {
        c[i] += x;
        c[i] %= mod;
    }
    sort(all(c));
    if(c == b) return 1;
    else return 0;
}
void solve() {
    read(n, mod);
    for(int i = 1; i <= n; ++i) {
        int x;
        read(x);
        a.pb(x);
    }
    for(int i = 1; i <= n; ++i) {
        int x;
        read(x);
        b.pb(x);
    }
    sort(all(a));
    sort(all(b));
    //print(a, b);
    int ans = MOD;
    for(int i = 0; i < n; ++i) {
        int x = (b[i] - a[i] + mod) % mod;
        //print(x);
        if(check(x)) {
            ans = min(ans, x);
        }
    }
    print(ans);
}
int main(){
    adskiy_razgon();
    ll t = 1;
    //read(t);
    FOR(t) {
        //write("Case ", i, ": ");
        solve();
    }
    r0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdb;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<string> vs;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdb> vpd;

#define mp make_pair
#define ff first
#define ss second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) (x).rbegin(), (x).rend()
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define vv <vector<vector> >
#define mp make_pair
#define str(a, b) substr((a), (b))
#define rr return
#define r0 return 0
#define mi map<int>
#define ms map<string>
#define ml map<ll>
#define all_sum(a,b) accumulate((a),(b),0)
#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
void adskiy_razgon(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1};
ll nod(ll a, ll b) {
    if(b > a) {
        swap(a, b);
    }
    while(b > 0) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll nok(ll a,ll b) {
    return a * b / nod(a, b);
}
void sp(ll a,double b) {
    cout << fixed << setprecision(a) << b;
}
ll binpow (ll a, ll n) {
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 1) {
        return binpow (a, n - 1) * a;
    }
    else {
        ll b = binpow (a, n / 2);
        return b * b;
    }
}

template<class A> void read(vector<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d=stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
    read(t...);
}
template<class A> void read(vector<A>& x) {
    EACH(a, x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    EACH(a, x)
        read(a);
}
string to_string(char c) {
    return string(1, c);
}
string to_string(bool b) {
    return b?"true":"false";
}
string to_string(const char* s) {
    return string(s);
}
string to_string(string s) {
    return s;
}
string to_string(vector<bool> v) {
    string res;
    FOR(sz(v))
        res+=char('0'+v[i]);
    return res;
}

template<size_t S> string to_string(bitset<S> b) {
    string res;
    FOR(S)
        res+=char('0'+b[i]);
    return res;
}
template<class T> string to_string(T v) {
    bool f=1;
    string res;
    EACH(x, v) {
        if(!f)
            res+=' ';
        f=0;
        res+=to_string(x);
    }
    return res;
}
template<class A> void write(A x) {
    cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) {
    write(h);
    write(t...);
}
void print() {
     write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) {
    write(h);
    if(sizeof...(t))
        write(' ');
    print(t...);
}

void yes(bool ok) {
    print((ok ? "YES" : "Yes"));
}
void no(bool ok) {
    print((ok ? "NO" : "No"));
}
const int MOD = 1e9+7; // 888244353;
const int N = 2e5+5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int M = 1005;
map<ll, ll> m;
int mod, n;
bool check(int x, map<ll,ll> y, vi a) {
    vi c = a;
    for(int i = 0; i < n; ++i) {
        c[i] += x;
        c[i] %= mod;
        if(y[c[i]]) y[c[i]]--;
        else return 0;
    }
    return 1;
}
void solve() {
    read(n, mod);
    vi a(n), b(n);
    for(int i = 0; i < n; ++i) read(a[i]);
    for(int i = 0; i < n; ++i) {
        read(b[i]);
        m[b[i]]++;
    }
    sort(all(b));
    sort(all(a));
    for(int i = 0; i < n; ++i) {
        if(check((b[i] - a[i] + mod) % mod, m, a)) {
            print(i);
            rr;
        }
    }
}
int main(){
    adskiy_razgon();
    ll t = 1;
    //read(t);
    FOR(t) {
        //write("Case ", i, ": ");
        solve();
    }
    r0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdb;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<string> vs;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdb> vpd;

#define mp make_pair
#define ff first
#define ss second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) (x).rbegin(), (x).rend()
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define vv <vector<vector> >
#define mp make_pair
#define str(a, b) substr((a), (b))
#define rr return
#define r0 return 0
#define mi map<int>
#define ms map<string>
#define ml map<ll>
#define all_sum(a,b) accumulate((a),(b),0)
#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
void adskiy_razgon(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1};
ll nod(ll a, ll b) {
    if(b > a) {
        swap(a, b);
    }
    while(b > 0) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll nok(ll a,ll b) {
    return a * b / nod(a, b);
}
void sp(ll a,double b) {
    cout << fixed << setprecision(a) << b;
}
ll binpow (ll a, ll n) {
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 1) {
        return binpow (a, n - 1) * a;
    }
    else {
        ll b = binpow (a, n / 2);
        return b * b;
    }
}

template<class A> void read(vector<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d=stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
    read(t...);
}
template<class A> void read(vector<A>& x) {
    EACH(a, x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    EACH(a, x)
        read(a);
}
string to_string(char c) {
    return string(1, c);
}
string to_string(bool b) {
    return b?"true":"false";
}
string to_string(const char* s) {
    return string(s);
}
string to_string(string s) {
    return s;
}
string to_string(vector<bool> v) {
    string res;
    FOR(sz(v))
        res+=char('0'+v[i]);
    return res;
}

template<size_t S> string to_string(bitset<S> b) {
    string res;
    FOR(S)
        res+=char('0'+b[i]);
    return res;
}
template<class T> string to_string(T v) {
    bool f=1;
    string res;
    EACH(x, v) {
        if(!f)
            res+=' ';
        f=0;
        res+=to_string(x);
    }
    return res;
}
template<class A> void write(A x) {
    cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) {
    write(h);
    write(t...);
}
void print() {
     write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) {
    write(h);
    if(sizeof...(t))
        write(' ');
    print(t...);
}

void yes(bool ok) {
    print((ok ? "YES" : "Yes"));
}
void no(bool ok) {
    print((ok ? "NO" : "No"));
}
const int MOD = 1e9+7; // 888244353;
const int N = 2e5+5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int M = 1005;
map<ll, ll> m;
ll mod, n;
bool check(ll x, map<ll,ll> y, vi a) {
    vi c = a;
    for(int i = 0; i < n; ++i) {
        c[i] += x;
        c[i] %= mod;
        if(y[c[i]]) y[c[i]]--;
        else return 0;
    }
    return 1;
}
void solve() {
    read(n, mod);
    vi a(n), b(n);
    for(int i = 0; i < n; ++i) read(a[i]);
    for(int i = 0; i < n; ++i) {
        read(b[i]);
        m[b[i]]++;
    }
    sort(all(b));
    sort(all(a));
    for(int i = 0; i < n; ++i) {
        if(check((b[i] - a[i] + mod) % mod, m, a)) {
            print((b[i] - a[i] + mod) % mod);
            rr;
        }
    }
}
int main(){
    adskiy_razgon();
    ll t = 1;
    //read(t);
    FOR(t) {
        //write("Case ", i, ": ");
        solve();
    }
    r0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdb;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<string> vs;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdb> vpd;

#define mp make_pair
#define ff first
#define ss second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) (x).rbegin(), (x).rend()
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define vv <vector<vector> >
#define mp make_pair
#define str(a, b) substr((a), (b))
#define rr return
#define r0 return 0
#define mi map<int>
#define ms map<string>
#define ml map<ll>
#define all_sum(a,b) accumulate((a),(b),0)
#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
void adskiy_razgon(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1};
ll nod(ll a, ll b) {
    if(b > a) {
        swap(a, b);
    }
    while(b > 0) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll nok(ll a,ll b) {
    return a * b / nod(a, b);
}
void sp(ll a,double b) {
    cout << fixed << setprecision(a) << b;
}
ll binpow (ll a, ll n) {
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 1) {
        return binpow (a, n - 1) * a;
    }
    else {
        ll b = binpow (a, n / 2);
        return b * b;
    }
}

template<class A> void read(vector<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d=stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
    read(t...);
}
template<class A> void read(vector<A>& x) {
    EACH(a, x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    EACH(a, x)
        read(a);
}
string to_string(char c) {
    return string(1, c);
}
string to_string(bool b) {
    return b?"true":"false";
}
string to_string(const char* s) {
    return string(s);
}
string to_string(string s) {
    return s;
}
string to_string(vector<bool> v) {
    string res;
    FOR(sz(v))
        res+=char('0'+v[i]);
    return res;
}

template<size_t S> string to_string(bitset<S> b) {
    string res;
    FOR(S)
        res+=char('0'+b[i]);
    return res;
}
template<class T> string to_string(T v) {
    bool f=1;
    string res;
    EACH(x, v) {
        if(!f)
            res+=' ';
        f=0;
        res+=to_string(x);
    }
    return res;
}
template<class A> void write(A x) {
    cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) {
    write(h);
    write(t...);
}
void print() {
     write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) {
    write(h);
    if(sizeof...(t))
        write(' ');
    print(t...);
}

void yes(bool ok) {
    print((ok ? "YES" : "Yes"));
}
void no(bool ok) {
    print((ok ? "NO" : "No"));
}
const int MOD = 1e9+7; // 888244353;
const int N = 2e5+5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int M = 1005;
map<ll, ll> m;
ll mod, n;
bool check(ll x, map<ll,ll> y, vl a) {
    vl c = a;
    for(int i = 0; i < n; ++i) {
        c[i] += x;
        if(c[i] != mod) c[i] %= mod;
        if(y[c[i]]) y[c[i]]--;
        else return 0;
    }
    return 1;
}
void solve() {
    read(n, mod);
    vl a(n), b(n);
    for(int i = 0; i < n; ++i) read(a[i]);
    for(int i = 0; i < n; ++i) {
        read(b[i]);
        m[b[i]]++;
    }
    sort(all(b));
    sort(all(a));
    for(int i = 0; i < n; ++i) {
        if(check((b[i] - a[i] + mod) % mod, m, a)) {
            print((b[i] - a[i] + mod) % mod);
            rr;
        }
    }
}
int main(){
    adskiy_razgon();
    ll t = 1;
    //read(t);
    FOR(t) {
        //write("Case ", i, ": ");
        solve();
    }
    r0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdb;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<string> vs;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdb> vpd;

#define mp make_pair
#define ff first
#define ss second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) (x).rbegin(), (x).rend()
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define vv <vector<vector> >
#define mp make_pair
#define str(a, b) substr((a), (b))
#define rr return
#define r0 return 0
#define mi map<int>
#define ms map<string>
#define ml map<ll>
#define all_sum(a,b) accumulate((a),(b),0)
#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
void adskiy_razgon(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1};
ll nod(ll a, ll b) {
    if(b > a) {
        swap(a, b);
    }
    while(b > 0) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll nok(ll a,ll b) {
    return a * b / nod(a, b);
}
void sp(ll a,double b) {
    cout << fixed << setprecision(a) << b;
}
ll binpow (ll a, ll n) {
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 1) {
        return binpow (a, n - 1) * a;
    }
    else {
        ll b = binpow (a, n / 2);
        return b * b;
    }
}

template<class A> void read(vector<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d=stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
    read(t...);
}
template<class A> void read(vector<A>& x) {
    EACH(a, x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    EACH(a, x)
        read(a);
}
string to_string(char c) {
    return string(1, c);
}
string to_string(bool b) {
    return b?"true":"false";
}
string to_string(const char* s) {
    return string(s);
}
string to_string(string s) {
    return s;
}
string to_string(vector<bool> v) {
    string res;
    FOR(sz(v))
        res+=char('0'+v[i]);
    return res;
}

template<size_t S> string to_string(bitset<S> b) {
    string res;
    FOR(S)
        res+=char('0'+b[i]);
    return res;
}
template<class T> string to_string(T v) {
    bool f=1;
    string res;
    EACH(x, v) {
        if(!f)
            res+=' ';
        f=0;
        res+=to_string(x);
    }
    return res;
}
template<class A> void write(A x) {
    cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) {
    write(h);
    write(t...);
}
void print() {
     write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) {
    write(h);
    if(sizeof...(t))
        write(' ');
    print(t...);
}

void yes(bool ok) {
    print((ok ? "YES" : "Yes"));
}
void no(bool ok) {
    print((ok ? "NO" : "No"));
}
const int MOD = 1e9+7; // 888244353;
const int N = 2e5+5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int M = 1005;
map<ll, ll> m;
vi a(N), b(N);
int mod, n;
bool check(int x, map<ll,ll> y) {
    vi c = a;
    for(int i = 0; i < n; ++i) {
        c[i] += x;
        c[i] %= mod;
        if(y[c[i]]) y[c[i]]--;
        else return 0;
    }
    return 1;
}
void solve() {
    read(n, mod);
    for(int i = 0; i < n; ++i) read(a[i]);
    for(int i = 0; i < n; ++i) {
        read(b[i]);
        m[b[i]]++;
    }
    for(int i = 0; i < mod; ++i) {
        if(check(i, m)) {
            print(i);
            rr;
        }
    }
}
int main(){
    adskiy_razgon();
    ll t = 1;
    //read(t);
    FOR(t) {
        //write("Case ", i, ": ");
        solve();
    }
    r0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdb;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<string> vs;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdb> vpd;

#define mp make_pair
#define ff first
#define ss second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) (x).rbegin(), (x).rend()
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define vv <vector<vector> >
#define mp make_pair
#define str(a, b) substr((a), (b))
#define rr return
#define r0 return 0
#define mi map<int>
#define ms map<string>
#define ml map<ll>
#define all_sum(a,b) accumulate((a),(b),0)
#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
void adskiy_razgon(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1};
ll nod(ll a, ll b) {
    if(b > a) {
        swap(a, b);
    }
    while(b > 0) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll nok(ll a,ll b) {
    return a * b / nod(a, b);
}
void sp(ll a,double b) {
    cout << fixed << setprecision(a) << b;
}
ll binpow (ll a, ll n) {
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 1) {
        return binpow (a, n - 1) * a;
    }
    else {
        ll b = binpow (a, n / 2);
        return b * b;
    }
}

template<class A> void read(vector<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d=stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
    read(t...);
}
template<class A> void read(vector<A>& x) {
    EACH(a, x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    EACH(a, x)
        read(a);
}
string to_string(char c) {
    return string(1, c);
}
string to_string(bool b) {
    return b?"true":"false";
}
string to_string(const char* s) {
    return string(s);
}
string to_string(string s) {
    return s;
}
string to_string(vector<bool> v) {
    string res;
    FOR(sz(v))
        res+=char('0'+v[i]);
    return res;
}

template<size_t S> string to_string(bitset<S> b) {
    string res;
    FOR(S)
        res+=char('0'+b[i]);
    return res;
}
template<class T> string to_string(T v) {
    bool f=1;
    string res;
    EACH(x, v) {
        if(!f)
            res+=' ';
        f=0;
        res+=to_string(x);
    }
    return res;
}
template<class A> void write(A x) {
    cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) {
    write(h);
    write(t...);
}
void print() {
     write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) {
    write(h);
    if(sizeof...(t))
        write(' ');
    print(t...);
}

void yes(bool ok) {
    print((ok ? "YES" : "Yes"));
}
void no(bool ok) {
    print((ok ? "NO" : "No"));
}
const int MOD = 1e9+7; // 888244353;
const int N = 2e5+5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int M = 1005;
vi a, b, c;
int n, mod;
bool check(int x) {
    c = a;
    for(int i = 0; i < sz(c); ++i) {
        c[i] += x;
        c[i] %= mod;
    }
    sort(all(c));
    if(c == b) return 1;
    else return 0;
}
void solve() {
    read(n, mod);
    for(int i = 1; i <= n; ++i) {
        int x;
        read(x);
        a.pb(x);
    }
    for(int i = 1; i <= n; ++i) {
        int x;
        read(x);
        b.pb(x);
    }
    sort(all(a));
    sort(all(b));
    //print(a, b);
    int ans = MOD;
    for(int i = 0; i < n; ++i) {
        int x = (b[i] - a[i]) % mod;
        //print(x);
        if(check(x)) {
            ans = min(ans, x);
        }
    }
    print(ans);
}
int main(){
    adskiy_razgon();
    ll t = 1;
    //read(t);
    FOR(t) {
        //write("Case ", i, ": ");
        solve();
    }
    r0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdb;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<string> vs;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdb> vpd;

#define mp make_pair
#define ff first
#define ss second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) (x).rbegin(), (x).rend()
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define vv <vector<vector> >
#define mp make_pair
#define str(a, b) substr((a), (b))
#define rr return
#define r0 return 0
#define mi map<int>
#define ms map<string>
#define ml map<ll>
#define all_sum(a,b) accumulate((a),(b),0)
#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
void adskiy_razgon(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1};
ll nod(ll a, ll b) {
    if(b > a) {
        swap(a, b);
    }
    while(b > 0) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll nok(ll a,ll b) {
    return a * b / nod(a, b);
}
void sp(ll a,double b) {
    cout << fixed << setprecision(a) << b;
}
ll binpow (ll a, ll n) {
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 1) {
        return binpow (a, n - 1) * a;
    }
    else {
        ll b = binpow (a, n / 2);
        return b * b;
    }
}

template<class A> void read(vector<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d=stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
    read(t...);
}
template<class A> void read(vector<A>& x) {
    EACH(a, x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    EACH(a, x)
        read(a);
}
string to_string(char c) {
    return string(1, c);
}
string to_string(bool b) {
    return b?"true":"false";
}
string to_string(const char* s) {
    return string(s);
}
string to_string(string s) {
    return s;
}
string to_string(vector<bool> v) {
    string res;
    FOR(sz(v))
        res+=char('0'+v[i]);
    return res;
}

template<size_t S> string to_string(bitset<S> b) {
    string res;
    FOR(S)
        res+=char('0'+b[i]);
    return res;
}
template<class T> string to_string(T v) {
    bool f=1;
    string res;
    EACH(x, v) {
        if(!f)
            res+=' ';
        f=0;
        res+=to_string(x);
    }
    return res;
}
template<class A> void write(A x) {
    cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) {
    write(h);
    write(t...);
}
void print() {
     write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) {
    write(h);
    if(sizeof...(t))
        write(' ');
    print(t...);
}

void yes(bool ok) {
    print((ok ? "YES" : "Yes"));
}
void no(bool ok) {
    print((ok ? "NO" : "No"));
}
const int MOD = 1e9+7; // 888244353;
const int N = 2e5+5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int M = 1005;
map<ll, ll> m;
vi a(N), b(N);
int mod, n;
bool check(int x) {
    vi c = a;
    for(int i = 0; i < n; ++i) {
        c[i] += x;
        c[i] %= mod;
    }
    for(int i = 0; i < n; ++i) {
        if(c[i] != b[i]) return 0;
    }
    return 1;
}
void solve() {
    read(n, mod);
    for(int i = 0; i < n; ++i) read(a[i]);
    for(int i = 0; i < n; ++i) read(b[i]);
    sort(all(a));
    sort(all(b));
    for(int i = 0; i < mod; ++i) {
        if(check(i)) {
            print(i);
            rr;
        }
    }
}
int main(){
    adskiy_razgon();
    ll t = 1;
    //read(t);
    FOR(t) {
        //write("Case ", i, ": ");
        solve();
    }
    r0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdb;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<string> vs;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdb> vpd;

#define mp make_pair
#define ff first
#define ss second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) (x).rbegin(), (x).rend()
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define vv <vector<vector> >
#define mp make_pair
#define str(a, b) substr((a), (b))
#define rr return
#define r0 return 0
#define mi map<int>
#define ms map<string>
#define ml map<ll>
#define all_sum(a,b) accumulate((a),(b),0)
#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
void adskiy_razgon(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1};
ll nod(ll a, ll b) {
    if(b > a) {
        swap(a, b);
    }
    while(b > 0) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll nok(ll a,ll b) {
    return a * b / nod(a, b);
}
void sp(ll a,double b) {
    cout << fixed << setprecision(a) << b;
}
ll binpow (ll a, ll n) {
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 1) {
        return binpow (a, n - 1) * a;
    }
    else {
        ll b = binpow (a, n / 2);
        return b * b;
    }
}

template<class A> void read(vector<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d=stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
    read(t...);
}
template<class A> void read(vector<A>& x) {
    EACH(a, x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    EACH(a, x)
        read(a);
}
string to_string(char c) {
    return string(1, c);
}
string to_string(bool b) {
    return b?"true":"false";
}
string to_string(const char* s) {
    return string(s);
}
string to_string(string s) {
    return s;
}
string to_string(vector<bool> v) {
    string res;
    FOR(sz(v))
        res+=char('0'+v[i]);
    return res;
}

template<size_t S> string to_string(bitset<S> b) {
    string res;
    FOR(S)
        res+=char('0'+b[i]);
    return res;
}
template<class T> string to_string(T v) {
    bool f=1;
    string res;
    EACH(x, v) {
        if(!f)
            res+=' ';
        f=0;
        res+=to_string(x);
    }
    return res;
}
template<class A> void write(A x) {
    cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) {
    write(h);
    write(t...);
}
void print() {
     write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) {
    write(h);
    if(sizeof...(t))
        write(' ');
    print(t...);
}

void yes(bool ok) {
    print((ok ? "YES" : "Yes"));
}
void no(bool ok) {
    print((ok ? "NO" : "No"));
}
const int MOD = 1e9+7; // 888244353;
const int N = 2e5+5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int M = 1005;
vi a, b, c;
int n, mod;
bool check(int x) {
    c = a;
    for(int i = 0; i < sz(c); ++i) {
        c[i] += x;
        c[i] %= mod;
    }
    sort(all(c));
    if(c == b) return 1;
    else return 0;
}
void solve() {
    read(n, mod);
    for(int i = 1; i <= n; ++i) {
        int x;
        read(x);
        a.pb(x);
    }
    for(int i = 1; i <= n; ++i) {
        int x;
        read(x);
        b.pb(x);
    }
    sort(all(a));
    sort(all(b));
    //print(a, b);
    int ans = MOD;
    for(int i = 0; i < n; ++i) {
        int x = (b[i] - a[0] + mod) % mod;
        //print(x);
        if(check(x)) {
            ans = min(ans, x);
        }
    }
    print(ans);
}
int main(){
    adskiy_razgon();
    ll t = 1;
    //read(t);
    FOR(t) {
        //write("Case ", i, ": ");
        solve();
    }
    r0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdb;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<string> vs;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdb> vpd;

#define mp make_pair
#define ff first
#define ss second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) (x).rbegin(), (x).rend()
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define vv <vector<vector> >
#define mp make_pair
#define str(a, b) substr((a), (b))
#define rr return
#define r0 return 0
#define mi map<int>
#define ms map<string>
#define ml map<ll>
#define all_sum(a,b) accumulate((a),(b),0)
#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
void adskiy_razgon(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1};
ll nod(ll a, ll b) {
    if(b > a) {
        swap(a, b);
    }
    while(b > 0) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll nok(ll a,ll b) {
    return a * b / nod(a, b);
}
void sp(ll a,double b) {
    cout << fixed << setprecision(a) << b;
}
ll binpow (ll a, ll n) {
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 1) {
        return binpow (a, n - 1) * a;
    }
    else {
        ll b = binpow (a, n / 2);
        return b * b;
    }
}

template<class A> void read(vector<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d=stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
    read(t...);
}
template<class A> void read(vector<A>& x) {
    EACH(a, x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    EACH(a, x)
        read(a);
}
string to_string(char c) {
    return string(1, c);
}
string to_string(bool b) {
    return b?"true":"false";
}
string to_string(const char* s) {
    return string(s);
}
string to_string(string s) {
    return s;
}
string to_string(vector<bool> v) {
    string res;
    FOR(sz(v))
        res+=char('0'+v[i]);
    return res;
}

template<size_t S> string to_string(bitset<S> b) {
    string res;
    FOR(S)
        res+=char('0'+b[i]);
    return res;
}
template<class T> string to_string(T v) {
    bool f=1;
    string res;
    EACH(x, v) {
        if(!f)
            res+=' ';
        f=0;
        res+=to_string(x);
    }
    return res;
}
template<class A> void write(A x) {
    cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) {
    write(h);
    write(t...);
}
void print() {
     write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) {
    write(h);
    if(sizeof...(t))
        write(' ');
    print(t...);
}

void yes(bool ok) {
    print((ok ? "YES" : "Yes"));
}
void no(bool ok) {
    print((ok ? "NO" : "No"));
}
const int MOD = 1e9+7; // 888244353;
const int N = 2e5+5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int M = 1005;
map<ll, ll> m;
int mod, n;
bool check(int x, map<ll,ll> y, vi a) {
    vi c = a;
    for(int i = 0; i < n; ++i) {
        c[i] += x;
        c[i] %= mod;
        if(y[c[i]]) y[c[i]]--;
        else return 0;
    }
    return 1;
}
void solve() {
    read(n, mod);
    vi a(n), b(n);
    for(int i = 0; i < n; ++i) read(a[i]);
    for(int i = 0; i < n; ++i) {
        read(b[i]);
        m[b[i]]++;
    }
    sort(all(b));
    sort(all(a));
    for(int i = 0; i < n; ++i) {
        if(check((b[i] - a[i] + mod) % mod, m, a)) {
            print((b[i] - a[i] + mod) % mod);
            rr;
        }
    }
}
int main(){
    adskiy_razgon();
    ll t = 1;
    //read(t);
    FOR(t) {
        //write("Case ", i, ": ");
        solve();
    }
    r0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdb;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<string> vs;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdb> vpd;

#define mp make_pair
#define ff first
#define ss second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) (x).rbegin(), (x).rend()
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define vv <vector<vector> >
#define mp make_pair
#define str(a, b) substr((a), (b))
#define rr return
#define r0 return 0
#define mi map<int>
#define ms map<string>
#define ml map<ll>
#define all_sum(a,b) accumulate((a),(b),0)
#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
void adskiy_razgon(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1};
ll nod(ll a, ll b) {
    if(b > a) {
        swap(a, b);
    }
    while(b > 0) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll nok(ll a,ll b) {
    return a * b / nod(a, b);
}
void sp(ll a,double b) {
    cout << fixed << setprecision(a) << b;
}
ll binpow (ll a, ll n) {
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 1) {
        return binpow (a, n - 1) * a;
    }
    else {
        ll b = binpow (a, n / 2);
        return b * b;
    }
}

template<class A> void read(vector<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d=stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
    read(t...);
}
template<class A> void read(vector<A>& x) {
    EACH(a, x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    EACH(a, x)
        read(a);
}
string to_string(char c) {
    return string(1, c);
}
string to_string(bool b) {
    return b?"true":"false";
}
string to_string(const char* s) {
    return string(s);
}
string to_string(string s) {
    return s;
}
string to_string(vector<bool> v) {
    string res;
    FOR(sz(v))
        res+=char('0'+v[i]);
    return res;
}

template<size_t S> string to_string(bitset<S> b) {
    string res;
    FOR(S)
        res+=char('0'+b[i]);
    return res;
}
template<class T> string to_string(T v) {
    bool f=1;
    string res;
    EACH(x, v) {
        if(!f)
            res+=' ';
        f=0;
        res+=to_string(x);
    }
    return res;
}
template<class A> void write(A x) {
    cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) {
    write(h);
    write(t...);
}
void print() {
     write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) {
    write(h);
    if(sizeof...(t))
        write(' ');
    print(t...);
}

void yes(bool ok) {
    print((ok ? "YES" : "Yes"));
}
void no(bool ok) {
    print((ok ? "NO" : "No"));
}
const int MOD = 1e9+7; // 888244353;
const int N = 2e5+5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int M = 1005;
map<ll, ll> m;
ll mod, n;
bool check(ll x, map<ll,ll> y, vi a) {
    vi c = a;
    for(int i = 0; i < n; ++i) {
        c[i] += x;
        c[i] %= mod;
        if(y[c[i]]) y[c[i]]--;
        else return 0;
    }
    return 1;
}
void solve() {
    read(n, mod);
    vl a(n), b(n);
    for(int i = 0; i < n; ++i) read(a[i]);
    for(int i = 0; i < n; ++i) {
        read(b[i]);
        m[b[i]]++;
    }
    sort(all(b));
    sort(all(a));
    for(int i = 0; i < n; ++i) {
        if(check((b[i] - a[i] + mod) % mod, m, a)) {
            print((b[i] - a[i] + mod) % mod);
            rr;
        }
    }
}
int main(){
    adskiy_razgon();
    ll t = 1;
    //read(t);
    FOR(t) {
        //write("Case ", i, ": ");
        solve();
    }
    r0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdb;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<string> vs;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdb> vpd;

#define mp make_pair
#define ff first
#define ss second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) (x).rbegin(), (x).rend()
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define vv <vector<vector> >
#define mp make_pair
#define str(a, b) substr((a), (b))
#define rr return
#define r0 return 0
#define mi map<int>
#define ms map<string>
#define ml map<ll>
#define all_sum(a,b) accumulate((a),(b),0)
#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
void adskiy_razgon(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1};
ll nod(ll a, ll b) {
    if(b > a) {
        swap(a, b);
    }
    while(b > 0) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll nok(ll a,ll b) {
    return a * b / nod(a, b);
}
void sp(ll a,double b) {
    cout << fixed << setprecision(a) << b;
}
ll binpow (ll a, ll n) {
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 1) {
        return binpow (a, n - 1) * a;
    }
    else {
        ll b = binpow (a, n / 2);
        return b * b;
    }
}

template<class A> void read(vector<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d=stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
    read(t...);
}
template<class A> void read(vector<A>& x) {
    EACH(a, x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    EACH(a, x)
        read(a);
}
string to_string(char c) {
    return string(1, c);
}
string to_string(bool b) {
    return b?"true":"false";
}
string to_string(const char* s) {
    return string(s);
}
string to_string(string s) {
    return s;
}
string to_string(vector<bool> v) {
    string res;
    FOR(sz(v))
        res+=char('0'+v[i]);
    return res;
}

template<size_t S> string to_string(bitset<S> b) {
    string res;
    FOR(S)
        res+=char('0'+b[i]);
    return res;
}
template<class T> string to_string(T v) {
    bool f=1;
    string res;
    EACH(x, v) {
        if(!f)
            res+=' ';
        f=0;
        res+=to_string(x);
    }
    return res;
}
template<class A> void write(A x) {
    cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) {
    write(h);
    write(t...);
}
void print() {
     write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) {
    write(h);
    if(sizeof...(t))
        write(' ');
    print(t...);
}

void yes(bool ok) {
    print((ok ? "YES" : "Yes"));
}
void no(bool ok) {
    print((ok ? "NO" : "No"));
}
const int MOD = 1e9+7; // 888244353;
const int N = 2e5+5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int M = 1005;
map<ll, ll> m;
vi a(N), b(N);
int mod, n;
bool check(int x, map<ll,ll> y) {
    vi c = a;
    for(int i = 0; i < n; ++i) {
        c[i] += x;
        c[i] %= mod;
        if(y[c[i]]) y[c[i]]--;
        else return 0;
    }
    return 1;
}
void solve() {
    read(n, mod);
    for(int i = 0; i < n; ++i) read(a[i]);
    for(int i = 0; i < n; ++i) {
        read(b[i]);
        m[b[i]]++;
    }
    sort(a, a + n);
    sort(b, b + n);
    for(int i = 0; i < mod; ++i) {
        if(check(i, m)) {
            print(i);
            rr;
        }
    }
}
int main(){
    adskiy_razgon();
    ll t = 1;
    //read(t);
    FOR(t) {
        //write("Case ", i, ": ");
        solve();
    }
    r0;
}


#include <bits/stdc++.h>

using namespace std;

#define lol long long

lol ab(lol n){
  return n<0?-n:n;
}

int main()
{
  lol n, m, in;
  vector <lol> A, B;
  scanf("%lld %lld", &n, &m);
  for (int i = 0;i < n;i++){
    scanf("%lld", &in);
    A.push_back(in);
  }
  for (int i = 0;i < n;i++){
    scanf("%lld", &in);
    B.push_back(in);
  }
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  lol ans = INT_MAX;
  set <lol> S;
  set <lol>::iterator it;
  vector <lol> d;
  for (int i = 0;i < n;i++){
    S.insert(ab(A[i]-B[0])%m);
  }
  for (it = S.begin();it != S.end();it++){
    d.push_back(*it);
  }
  //for (int i = 0;i < d.size();i++)printf("%d ", d[i]);puts("");
  int len = d.size();
  for (int i = 0;i < len;i++){
    vector <lol> tmp;
    for (int j = 0;j < n;j++){
      tmp.push_back((A[j]+d[i])%m);
    }
    sort(tmp.begin(), tmp.end());
    //for (int j = 0;j < tmp.size();j++)printf("%d ", tmp[i]);puts("");
    if (tmp == B){
      ans = d[i];
      break;
    }
  }
  printf("%lld\n", ans);
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define lol long long

lol ab(lol n){
  return n<0?-n:n;
}

int main()
{
  lol n, m, in;
  vector <lol> A, B;
  scanf("%lld %lld", &n, &m);
  for (int i = 0;i < n;i++){
    scanf("%lld", &in);
    A.push_back(in);
  }
  for (int i = 0;i < n;i++){
    scanf("%lld", &in);
    B.push_back(in);
  }
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  lol ans = INT_MAX;
  set <lol> S;
  set <lol>::iterator it;
  vector <lol> d;
  for (int i = 0;i < n;i++){
    S.insert(ab(A[i]-B[0]));
  }
  for (it = S.begin();it != S.end();it++){
    d.push_back(*it);
  }
  //for (int i = 0;i < d.size();i++)printf("%d ", d[i]);puts("");
  int len = d.size();
  for (int i = 0;i < len;i++){
    vector <lol> tmp;
    for (int j = 0;j < n;j++){
      tmp.push_back((A[j]+d[i])%m);
    }
    sort(tmp.begin(), tmp.end());
    //for (int j = 0;j < tmp.size();j++)printf("%d ", tmp[i]);puts("");
    if (tmp == B){
      ans = d[i];
      break;
    }
  }
  printf("%lld\n", ans);
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int ab(int n){
  return n<0?-n:n;
}

int main()
{
  int n, m, in;
  vector <int> A, B;
  scanf("%d %d", &n, &m);
  for (int i = 0;i < n;i++){
    scanf("%d", &in);
    A.push_back(in);
  }
  for (int i = 0;i < n;i++){
    scanf("%d", &in);
    B.push_back(in);
  }
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  int ans = INT_MAX;
  set <int> S;
  set <int>::iterator it;
  vector <int> d;
  for (int i = 0;i < n;i++){
    S.insert(ab(A[i]-B[i]));
  }
  for (it = S.begin();it != S.end();it++){
    d.push_back(*it);
  }
  //for (int i = 0;i < d.size();i++)printf("%d ", d[i]);puts("");
  int len = d.size();
  for (int i = 0;i < len;i++){
    vector <int> tmp;
    for (int j = 0;j < n;j++){
      tmp.push_back((A[j]+d[i])%m);
    }
    sort(tmp.begin(), tmp.end());
    //for (int j = 0;j < tmp.size();j++)printf("%d ", tmp[i]);puts("");
    if (tmp == B){
      ans = d[i];
      break;
    }
  }
  printf("%d\n", ans);
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define lol long long

lol ab(lol n){
  return n<0?-n:n;
}

int main()
{
  lol n, m, in;
  vector <lol> A, B;
  scanf("%lld %lld", &n, &m);
  for (int i = 0;i < n;i++){
    scanf("%lld", &in);
    A.push_back(in);
  }
  for (int i = 0;i < n;i++){
    scanf("%lld", &in);
    B.push_back(in);
  }
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  lol ans = INT_MAX;
  set <lol> S;
  set <lol>::iterator it;
  vector <lol> d;
  for (int i = 0;i < n;i++){
    S.insert(ab(A[i]-B[i]));
  }
  for (it = S.begin();it != S.end();it++){
    d.push_back(*it);
  }
  //for (int i = 0;i < d.size();i++)printf("%d ", d[i]);puts("");
  int len = d.size();
  for (int i = 0;i < len;i++){
    vector <lol> tmp;
    for (int j = 0;j < n;j++){
      tmp.push_back((A[j]+d[i])%m);
    }
    sort(tmp.begin(), tmp.end());
    //for (int j = 0;j < tmp.size();j++)printf("%d ", tmp[i]);puts("");
    if (tmp == B){
      ans = d[i];
      break;
    }
  }
  printf("%lld\n", ans);
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int ab(int n){
  return n<0?-n:n;
}

int main()
{
  int n, m, in;
  vector <int> A, B;
  scanf("%d %d", &n, &m);
  for (int i = 0;i < n;i++){
    scanf("%d", &in);
    A.push_back(in);
  }
  for (int i = 0;i < n;i++){
    scanf("%d", &in);
    B.push_back(in);
  }
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  int d = 0;
  for (int i = 0;i < n;i++){
    d = max(d, ab(A[i]-B[i]));
  }
  printf("%d\n", d);
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int ab(int n){
  return n<0?-n:n;
}

int main()
{
  int n, m, in;
  vector <int> A, B;
  scanf("%d %d", &n, &m);
  for (int i = 0;i < n;i++){
    scanf("%d", &in);
    A.push_back(in);
  }
  for (int i = 0;i < n;i++){
    scanf("%d", &in);
    B.push_back(in);
  }
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  int ans = INT_MAX;
  set <int> S;
  set <int>::iterator it;
  vector <int> d;
//  for (int i = 0;i < n;i++){
//    S.insert(ab(A[i]-B[i]));
//  }
//  for (it = S.begin();it != S.end();it++){
//    d.push_back(*it);
//  }
  //for (int i = 0;i < d.size();i++)printf("%d ", d[i]);puts("");
  int len = d.size();
  for (int i = 0;i < m;i++){
    vector <int> tmp;
    for (int j = 0;j < n;j++){
      tmp.push_back((A[j]+i)%m);
    }
    sort(tmp.begin(), tmp.end());
    //for (int j = 0;j < tmp.size();j++)printf("%d ", tmp[i]);puts("");
    if (tmp == B){
      ans = i;
      break;
    }
  }
  printf("%d\n", ans);
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define lol long long

lol ab(lol n){
  return n<0?-n:n;
}

int main()
{
  lol n, m, in;
  vector <lol> A, B;
  scanf("%lld %lld", &n, &m);
  for (int i = 0;i < n;i++){
    scanf("%lld", &in);
    A.push_back(in);
  }
  for (int i = 0;i < n;i++){
    scanf("%lld", &in);
    B.push_back(in);
  }
  //sort(A.begin(), A.end());
  //sort(B.begin(), B.end());
  lol ans = INT_MAX;
  set <lol> S;
  set <lol>::iterator it;
  vector <lol> d;
  for (int i = 0;i < n;i++){
    int x = B[n-1]-A[i];
    if (x < 0)x += m;
    S.insert(x);
  }
  for (it = S.begin();it != S.end();it++){
    d.push_back(*it);
  }
  //for (int i = 0;i < d.size();i++)printf("%d ", d[i]);puts("");
  int len = d.size();
  sort(B.begin(), B.end());
  for (int i = 0;i < len;i++){
    vector <lol> tmp;
    for (int j = 0;j < n;j++){
      tmp.push_back((A[j]+d[i])%m);
    }
    sort(tmp.begin(), tmp.end());
    //for (int j = 0;j < tmp.size();j++)printf("%d ", tmp[i]);puts("");
    if (tmp == B){
      ans = d[i];
      break;
    }
  }
  printf("%lld\n", ans);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define int long long

#define all(x) (x).begin(), (x).end()

const int N = 3e5 + 2;
const int MOD = 1e9 + 9;
const int P = 17917917;

int a[N];

vector<pair<int, int>> comp(int l, int r) {
    vector<pair<int, int>> st;
    for (int i = l; i <= r; ++i) {
        if (st.empty() || st.back().first != a[i]) {
            st.emplace_back(a[i], 1);
        } else {
            st.back().second++;
        }
    }
    return st;
}

int f(int l, int r) {
    if (l + 1 == r) {
        return 0;
    }
    int ans = 0;

    int mid = (l + r) / 2;
    auto x = comp(l, mid - 1);
    auto y = comp(mid, r - 1);
    vector<pair<int, int>> h;
    int n = y.size();
    int cur = 0;
    int fr = -1;
    for (int i = 0; i < n; ++i) {

        if (y[i].second % 2 == 1) {
            cur = cur * P + y[i].first;
            cur %= MOD;
            h.emplace_back(cur, (y[i].second + 1) / 2);
            if (i > 0 && y[i - 1].second % 2 == 1) {
                h[i - 1].second += y[i].second / 2;
            }
        } else {
            h.emplace_back((cur * P + y[i].first) % MOD, y[i].second / 2);
            if (fr != -1) {
                h[fr].second += y[i].second / 2;
            }
        }

        if (y[i].second % 2 == 1) {
            fr = i;
        }
    }
    sort(all(h));
    h.erase(unique(all(h)), h.end());
    n = x.size();
    cur = 0;
    int ans_last = 0;
    for (int i = n - 1; i >= 0; --i) {
        int kek = ans;
        int pre = cur;

        cur = cur * P + x[i].first;
        cur %= MOD;
        auto it = lower_bound(all(h), make_pair(cur, -1LL));
        if (it != h.end() && it->first == cur) {
            ans += (it->second) * ((x[i].second + 1) / 2);
        }

        if (x[i].second % 2 == 0) {
            cur = pre;
        }

        ans += ans_last * (x[i].second / 2);
        ans_last = ans - kek;
    }

    return ans + f(l, mid) + f(mid, r);
}

void solve() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    cout << f(0, n) << '\n';
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}


#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()

struct node {
    map<int, node*> a;
    node *par;
    int cnt;
};

const int MEM = 5e6;
int _ptr = 0;

node mem[MEM];

node *new_node(node *v) {
    mem[_ptr].par = v;
    return mem + _ptr++;
}

node *go(node *v, int x) {
    if (v->a.count(x)) {
        v = v->a[x];
    } else {
        v->a[x] = new_node(v);
        v = v->a[x];
    }
    return v;
}

long long solve(int l, int r, vector<int> &a) {
    if (l == r) {
        return 0;
    }
    int m = (l + r) / 2;
    long long ans = solve(l, m, a) + solve(m + 1, r, a);

    vector<int> st;
    node *root = new_node(nullptr);
    node *cur = root;
    for (int i = m + 1; i <= r; ++i) {
        if (st.empty() || st.back() != a[i]) {
            st.emplace_back(a[i]);
            cur = go(cur, a[i]);
            cur->cnt++;
        } else {
            st.pop_back();
            cur = cur->par;
            cur->cnt++;
        }
    }
    st.clear();
    cur = root;
    for (int i = m; i >= l; --i) {
        if (st.empty() || st.back() != a[i]) {
            st.emplace_back(a[i]);
            cur = go(cur, a[i]);
            ans += cur->cnt;
        } else {
            st.pop_back();
            cur = cur->par;
            ans += cur->cnt;
        }
    }
    return ans;
}

void $main() {
    int n;
    cin >> n;
    vector<int> v(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i];
    }
    cout << solve(0, n - 1, v) << '\n';
}

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;
    while (t--) {
        $main();
    }
}


#include <bits/stdc++.h>
#define ll long long
#define fr first
#define sc second
#define ii pair<int,int>
#define mp make_pair
using namespace std;
int n,q;
int a[500000];
ll ans = 0 , prime = 1009 ,mod = 1e9+9 ;
vector < ll > v;
void divide(int L,int R)
{
    if(L == R)
        return ;
    int mid = (L+R)/2;
    v.clear();
    v.push_back(0);
    map < ll , ll > R1;
    stack<ll> st1;
    for(int i=mid; i>=L; i--)
    {
        if(st1.size() > 0)
        {
            int top = st1.top();
            if(top == a[i])
            {
                st1.pop();
                v.pop_back();
            }
            else
            {
                st1.push(a[i]);
                int len = (int)v.size()-1;
                ll last = v[len];
                v.push_back((last*prime+a[i])%mod);
            }
        }
        else
        {
            st1.push(a[i]);
            ll last=0;
            int len = (int)v.size()-1;
            last = v[len];
            v.push_back((last*prime+a[i])%mod);
        }
        ll len = (int)v.size()-1;
        ll val = v[len];
        R1[val]++;
    }
    v.clear();
    while(st1.size()>0)
        st1.pop();
    v.push_back(0);
    for(int i=mid+1; i<=R; i++)
    {
        if(st1.size() > 0)
        {
            int top = st1.top();
            if(top == a[i])
            {
                st1.pop();
                v.pop_back();
            }
            else
            {
                st1.push(a[i]);
                int len = (int)v.size()-1;
                ll last = v[len];
                v.push_back((last*prime+a[i])%mod);
            }
        }
        else
        {
            st1.push(a[i]);
            ll last=0;
            int len = (int)v.size()-1;
            last = v[len];
            v.push_back((last*prime+a[i])%mod);
        }
        ll len = (int)v.size()-1;
        ll val = v[len];
        ans+=R1[val];
    }
    v.clear();
    divide(L,mid);
    divide(mid+1,R);
}
void solve()
{
    cin>>n;
    for(int i=1; i<=n; i++)
        scanf("%d",&a[i]);
    ans = 0;
    divide(1,n);
    cout<<ans<<endl;
}
int main()
{
    cin>>q;
    while(q--)
        solve();
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define fr first
#define sc second
#define ii pair<int,int>
#define mp make_pair
using namespace std;
int n,q;
int a[500000];
ll ans = 0 , prime = 1009 ,mod = 1e9+9 ;
vector < ll > v;
void divide(int L,int R)
{
    if(L == R)
        return ;
    int mid = (L+R)/2;
    v.clear();
    v.push_back(0);
    map < pair<ll,ll> , ll > R1;
    stack<ll> st1;
    for(int i=mid; i>=L; i--)
    {
        if(st1.size() > 0)
        {
            int top = st1.top();
            if(top == a[i])
            {
                st1.pop();
                v.pop_back();
            }
            else
            {
                st1.push(a[i]);
                int len = (int)v.size()-1;
                ll last = v[len];
                v.push_back((last*prime+a[i])%mod);
            }
        }
        else
        {
            st1.push(a[i]);
            ll last=0;
            int len = (int)v.size()-1;
            last = v[len];
            v.push_back((last*prime+a[i])%mod);
        }
        ll len = (int)v.size()-1;
        ll val = v[len];
        R1[mp(val,len)]++;
    }
    v.clear();
    while(st1.size()>0)
        st1.pop();
    v.push_back(0);
    for(int i=mid+1; i<=R; i++)
    {
        if(st1.size() > 0)
        {
            int top = st1.top();
            if(top == a[i])
            {
                st1.pop();
                v.pop_back();
            }
            else
            {
                st1.push(a[i]);
                int len = (int)v.size()-1;
                ll last = v[len];
                v.push_back((last*prime+a[i])%mod);
            }
        }
        else
        {
            st1.push(a[i]);
            ll last=0;
            int len = (int)v.size()-1;
            last = v[len];
            v.push_back((last*prime+a[i])%mod);
        }
        ll len = (int)v.size()-1;
        ll val = v[len];
        ans+=R1[mp(val,len)];
    }
    v.clear();
    divide(L,mid);
    divide(mid+1,R);
}
void solve()
{
    cin>>n;
    for(int i=1; i<=n; i++)
        scanf("%d",&a[i]);
    ans = 0;
    divide(1,n);
    cout<<ans<<endl;
}
int main()
{
    cin>>q;
    while(q--)
        solve();
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define fr first
#define sc second
#define ii pair<int,int>
#define mp make_pair
using namespace std;
int n,q;
int a[500000];
ll ans = 0 , prime = 1009 ,mod = 1e9+7 ;
vector < ll > v;
void divide(int L,int R)
{
    if(L == R)
        return ;
    int mid = (L+R)/2;
    v.clear();
    v.push_back(0);
    map < ll , ll > R1;
    stack<ll> st1;
    for(int i=mid; i>=L; i--)
    {
        if(st1.size() > 0)
        {
            int top = st1.top();
            if(top == a[i])
            {
                st1.pop();
                v.pop_back();
            }
            else
            {
                st1.push(a[i]);
                int len = (int)v.size()-1;
                ll last = v[len];
                v.push_back((last*prime+a[i])%mod);
            }
        }
        else
        {
            st1.push(a[i]);
            ll last=0;
            int len = (int)v.size()-1;
            if(len >=0)
                last = v[len];
            v.push_back((last*prime+a[i])%mod);
        }
        ll len = (int)v.size()-1;
        ll val = v[len];
        R1[val]++;
    }
    v.clear();
    while(st1.size()>0)
        st1.pop();
    v.push_back(0);
    for(int i=mid+1; i<=R; i++)
    {
        if(st1.size() > 0)
        {
            int top = st1.top();
            if(top == a[i])
            {
                st1.pop();
                v.pop_back();
            }
            else
            {
                st1.push(a[i]);
                int len = (int)v.size()-1;
                ll last = v[len];
                v.push_back((last*prime+a[i])%mod);
            }
        }
        else
        {
            st1.push(a[i]);
            ll last=0;
            int len = (int)v.size()-1;
            if(len >=0)
                last = v[len];
            v.push_back((last*prime+a[i])%mod);
        }
        ll len = (int)v.size()-1;
        ll val = v[len];
        ans+=R1[val];
    }
    v.clear();
    divide(L,mid);
    divide(mid+1,R);
}
void solve()
{
    cin>>n;
    for(int i=1; i<=n; i++)
        scanf("%d",&a[i]);
    ans = 0;
    divide(1,n);
    cout<<ans<<endl;
}
int main()
{
    cin>>q;
    while(q--)
        solve();
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define fr first
#define sc second
#define ii pair<int,int>
#define mp make_pair
using namespace std;
int n,q;
int a[500000];
ll ans = 0 , prime = 1013 ,mod = 1e9+7 ;
vector < ll > v;
void divide(int L,int R)
{
    if(L == R)
        return ;
    int mid = (L+R)/2;
    v.clear();
    map < ll , ll > R1;
    stack<ll> st1;
    for(int i=mid; i>=L; i--)
    {
        if(st1.size() > 0)
        {
            int top = st1.top();
            if(top == a[i])
            {
                st1.pop();
                v.pop_back();
            }
            else
            {
                st1.push(a[i]);
                int len = v.size()-1;
                ll last = v[len];
                v.push_back((last*prime+a[i])%mod);
            }
        }
        else
        {
            st1.push(a[i]);
            ll last=0;
            int len = v.size()-1;
            if(len >=0)
                last = v[len];
            v.push_back((last*prime+a[i])%mod);
        }
        ll len = v.size()-1;
        ll val = v[len];
        R1[val]++;
    }
    v.clear();
    while(st1.size()>0)
        st1.pop();
    for(int i=mid+1; i<=R; i++)
    {
        if(st1.size() > 0)
        {
            int top = st1.top();
            if(top == a[i])
            {
                st1.pop();
                v.pop_back();
            }
            else
            {
                st1.push(a[i]);
                int len = v.size()-1;
                ll last = v[len];
                v.push_back((last*prime+a[i])%mod);
            }
        }
        else
        {
            st1.push(a[i]);
            ll last=0;
            int len = v.size()-1;
            if(len >=0)
                last = v[len];
            v.push_back((last*prime+a[i])%mod);
        }
        ll len = v.size()-1;
        ll val = v[len];
        ans+=R1[val];
    }
    divide(L,mid);
    divide(mid+1,R);
}
void solve()
{
    cin>>n;
    for(int i=1; i<=n; i++)
        scanf("%d",&a[i]);
    ans = 0;
    divide(1,n);
    cout<<ans<<endl;
}
int main()
{
    cin>>q;
    while(q--)
        solve();
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define fr first
#define sc second
#define ii pair<int,int>
#define mp make_pair
using namespace std;
int n,q;
int a[500000];
ll ans = 0 , prime = 1e9+7 ,mod = 1e9+9 ;
vector < ll > v;
void divide(int L,int R)
{
    if(L == R)
        return ;
    int mid = (L+R)/2;
    v.clear();
    v.push_back(0);
    map < ll , ll > R1;
    stack<ll> st1;
    for(int i=mid; i>=L; i--)
    {
        if(st1.size() > 0)
        {
            int top = st1.top();
            if(top == a[i])
            {
                st1.pop();
                v.pop_back();
            }
            else
            {
                st1.push(a[i]);
                int len = (int)v.size()-1;
                ll last = v[len];
                v.push_back((last*prime+a[i])%mod);
            }
        }
        else
        {
            st1.push(a[i]);
            ll last=0;
            int len = (int)v.size()-1;
            last = v[len];
            v.push_back((last*prime+a[i])%mod);
        }
        ll len = (int)v.size()-1;
        ll val = v[len];
        R1[val]++;
    }
    v.clear();
    while(st1.size()>0)
        st1.pop();
    v.push_back(0);
    for(int i=mid+1; i<=R; i++)
    {
        if(st1.size() > 0)
        {
            int top = st1.top();
            if(top == a[i])
            {
                st1.pop();
                v.pop_back();
            }
            else
            {
                st1.push(a[i]);
                int len = (int)v.size()-1;
                ll last = v[len];
                v.push_back((last*prime+a[i])%mod);
            }
        }
        else
        {
            st1.push(a[i]);
            ll last=0;
            int len = (int)v.size()-1;
            last = v[len];
            v.push_back((last*prime+a[i])%mod);
        }
        ll len = (int)v.size()-1;
        ll val = v[len];
        ans+=R1[val];
    }
    v.clear();
    divide(L,mid);
    divide(mid+1,R);
}
void solve()
{
    cin>>n;
    for(int i=1; i<=n; i++)
        scanf("%d",&a[i]);
    ans = 0;
    divide(1,n);
    cout<<ans<<endl;
}
int main()
{
    cin>>q;
    while(q--)
        solve();
    return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
using ll = long long;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define show(x) true
#endif
struct Aho{
	using NP = Aho*;
	map<int,NP> nx;
	NP p = nullptr;
	ll cnt = 0;

	NP add(int c){
		if(nx[c] == nullptr){
			nx[c] = new Aho();
			nx[c] -> p = this;
		}
		return nx[c];
	}
	void Vanish(){
		for(auto it: nx){
			it.sc -> Vanish();
		}
		delete this;
	}
};
stack<int> s;

ll solve(){
	int N;
	cin >> N;
	V<int> a(N); rep(i,N) cin >> a[i];

	function<ll(int,int)> f = [&](int l,int r){
		if(r-l == 1) return 0LL;
		int m = (l+r)/2;
		ll res = f(l,m) + f(m,r);

		auto T = new Aho();
		auto now = T;

		while(!s.empty()) s.pop();
		s.push(-1);
		for(int i=m-1;i>=l;i--){
			if(s.top() == a[i]){
				s.pop();
				now = now -> p;
			}else{
				s.push(a[i]);
				now = now->add(a[i]);
			}
			now -> cnt ++;
		}

		now = T;
		while(!s.empty()) s.pop();
		s.push(-1);
		for(int i=m;i<r;i++){
			if(s.top() == a[i]){
				s.pop();
				now = now -> p;
			}else{
				s.push(a[i]);
				now = now->add(a[i]);
			}
			res += now->cnt;
		}
		T -> Vanish();
//		show("--------------------");show(l);show(r);show(res);
		return res;
	};

	return f(0,N);
}
int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);		//DON'T USE scanf/printf/puts !!
	cout << fixed << setprecision(20);

	int T;
	cin >> T;
	rep(_,T) cout << solve() << '\n';
}