#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 100010
struct node{
int x,y;
}a[N];
int n,n1,n2,pos,c[N];
inline bool cmp(const node &p,const node &q){
return p.y>q.y;
}
int lowbit(int x){
return x&-x;
}
inline void updata(int p){
for(int i=p;i<=n;i+=lowbit(i)) c[i]+=1;
}
inline int query(int p){
int ans(0);
for(int i=p;i;i-=lowbit(i)) ans+=c[i];
return ans;
}
int main(){
scanf("%d%d",&n1,&n2);n=n1+n2;pos=n1;
for(int i=n1;i;i--) scanf("%d",&a[i].y);
for(int i=n1+1;i<=n;i++) scanf("%d",&a[i].y);
for(int i=1;i<=n;i++) a[i].x=i;
sort(a+1,a+n+1,cmp);
long long ans(0);
for(int i=1;i<=n;i++){
if(a[i].x<=pos) ans+=pos-a[i].x-query(pos)+query(a[i].x),pos=a[i].x;
else ans+=a[i].x-1-pos-query(a[i].x-1)+query(pos),pos=a[i].x-1;
updata(a[i].x);
}
printf("%lld\n",ans);
return 0;
}