1. int n;

  2. int a[400005];

  3. long long aux[22][22];

  4. long long sump[22];

  5. long long dp[(1<<20) + 5];


  6. int main() {

  7. cin>>n;

  8. for (int i=1; i<=n; ++i) {

  9. cin>>a[i];

  10. ++sump[a[i]];

  11. for (int j=1; j<=20; ++j)

  12. if (j!=a[i]) aux[a[i]][j]+=sump[j];

  13. }


  14. for (int i=1; i<(1<<20); ++i) {

  15. dp[i]=(long long)1000000000 * (long long)1000000000;


  16. for (int j=0; j<20; ++j)

  17. if (i&(1<<j)) {

  18. long long dpaux = 0;

  19. for (int k=0; k<20; ++k)

  20. if (k!=j && (i&(1<<k))) dpaux+=aux[j+1][k+1];

  21. dp[i]=min(dp[i],dpaux+dp[i-(1<<j)]);

  22. }

  23. }


  24. cout<<dp[(1<<20)-1];


  25. return 0;

  26. }