#include <bits/stdc++.h>
using namespace std;
#define inf 1e15
#define ll long long int


void solve(ll n, ll k){
     vector<ll>a(n);
         vector<ll>mp(n,0);
       
        for(int i=0;i<n;++i){
            cin>>a[i];
            ++mp[a[i]];
        }
        ll i=0;
        ll res = 0;
    
        while(i<n){
            if(mp[i]==0){
                break;
            }
            if(mp[i]<k){
                res += (k-mp[i])*i;
                k = mp[i];
            }
            ++i;
        }
        res += k*i;
        cout<<res;
    
}
int main() {
 
        int n,k;
        cin>>n>>k;
        solve(n,k);
     
}
#include<bits/stdc++.h>
using namespace std;


typedef vector <int> vi;
typedef pair< int ,int > pii;
#define endl "\n"
#define sd(val) scanf("%d",&val)
#define ss(val) scanf("%s",&val)
#define sl(val) scanf("%lld",&val)
#define debug(val) printf("check%d\n",val)
#define all(v) v.begin(),v.end()
#define PB push_back
#define MP make_pair
#define FF first
#define SS second
#define ll long long
#define MOD 1000000007
#define clr(val) memset(val,0,sizeof(val))
#define what_is(x) cerr << #x << " is " << x << endl; 
#define OJ \
    freopen("input.txt", "r", stdin); \
    freopen("output.txt", "w", stdout);
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);


int main() 
{
 
    int N;
    int x;

    cin >> N;

    int a[N], b[N];

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

    for (int i = 0; i < N; i++){
        cin >> b[i];
    } 

    int* a_max = max_element(a, a + N);
    int* b_min = min_element(b, b + N);

    x = *b_min - *a_max;

    if (x < 0){
        cout << 0;
    }else{
        cout << x + 1<< endl;
    }
    
    return 0;
   
}