Code:
#include<bits/stdc++.h>
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 2000000
using namespace std;
int n,m;
int A[maxn],B[maxn];
char arr[maxn],brr[maxn];
queue<int>q[30];
struct BIT
{
int C[maxn];
int lowbit(int t) { return t & (-t); }
void update(int x,int delta)
{
while(x<=m) C[x]+=delta,x+=lowbit(x);
}
int query(int x)
{
int tmp=0;
while(x>0) tmp+=C[x],x-=lowbit(x);
return tmp;
}
}t;
int main()
{
// setIO("input");
scanf("%d%s%s",&n,arr+1,brr+1);
int i,j;
for(i=1;i<=n;++i) A[i]=arr[i]-'A'+1;
for(i=1;i<=n;++i) B[i]=brr[i]-'A'+1;
for(i=1;i<=n;++i) q[B[i]].push(i);
for(i=1;i<=n;++i)
{
int c=A[i];
A[i]=q[A[i]].front();
q[c].pop();
}
m=n;
long long re=0;
for(i=1;i<=n;++i)
{
re+=t.query(m)-t.query(A[i]);
t.update(A[i],1);
}
printf("%lld\n",re);
return 0;
}