#include<bits/stdc++.h>
#include<string>
#define FIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ll long long
#define mod (ll)(1e9+7)
#define nax (ll)(1e5 + 10)
#define INFL LLONG_MAX
#define NINFL LLONG_MIN
#define INF INT_MAX
#define disp(a,n,arr) for(int i=a;i<(ll)n;i++) cout << arr[i] << " "; cout << "\n";
#define check cout << "Working\n";
#define pi pair<int,int>
#define pl pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define vpl vector<pl>
#define vpi vector<pi>
#define vb vector<bool>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) a.begin(),a.end()
using namespace std;

string sol(string number, int divisor)
{
    
    string ans;
    int idx = 0;
    int temp = number[idx] - '0';
    while (temp < divisor)
        temp = temp * 10 + (number[++idx] - '0');
 
    while (number.size() > idx) {
        ans += (temp / divisor) + '0';
 
        temp = (temp % divisor) * 10 + number[++idx] - '0';
    }
 
    
    if (ans.length() == 0)
        return "0";
 
    
    return ans;
}

int main() {
	FIO;
	int t = 1;
	//cin >> t;
	while(t--){
		ll n;
        cin >> n;
        map<ll,ll> mpp;
        ll ans = 0;
        for(int i=0;i<n;i++){
            ll a;
            cin >> a;
            ans += mpp[a%200];
            mpp[a%200]++;
        }
        cout << ans << "\n";
	}


	return 0;
}

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<cmath>
#include<cstdio>
#include<tuple>
#include<bitset>
#include<map>

using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define ALL(x) x.begin(),x.end()
#define debug(output) cout<<#output<<"= "<<output<<endl

using P=pair<int,int>;
using lint=long long;
using ll=long long;
const lint inf=1e18+7;
const int MOD=1000000007;
int n,l;
int calc(vector<int>& a,vector<int> b){
  auto prev=a;
  rep(i,n+1){
    a[i]+=n-i;
  }
  int res=0;
  int add=0;//a[i]からは引き、b[i]には足す
  for(int i=n;i>0;--i){
    auto it=upper_bound(a.begin(),a.end(),b[i]+add);
    --it;
    int ind=it-a.begin();
    if(ind>i){
      ++add;
      continue;
    }
    if(*it==b[i]+add){
      int last=*it;
      res+=i-ind;
      while(*it!=a[i]){
        ++it;
        *it=last;
      }
      a[i]=last;

    }
    else{
      ;
    }
    ++add;
  }
  rep(i,n+1){
    a[i]-=n-i;
  }
  return res;
}
signed main(){
  cin>>n>>l;
  lint res=0;
  vector<lint> a(n+1,0),b(n+1,0);
  rep1(i,n)cin>>a[i];
  rep1(i,n)cin>>b[i];
  a.push_back(l+1);
  b.push_back(l+1);

  res+=calc(a,b);
  reverse(a.begin(),a.end());
  reverse(b.begin(),b.end());
  for(auto& val:a){
    val=l+1-val;
  }
  for(auto& val:b){
    val=l+1-val;
  }
  res+=calc(a,b);
  rep1(i,n){
    if(a[i]!=b[i])res=-1;
  }
  cout<<res<<"\n";
  return 0;
}
