//#include "wiring.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
long long min_total_length(std::vector<int> r, std::vector<int> b) {
// either same as before or one more than the prevoius one
if(r.size()>b.size()){
swap(r,b);
}
// r ni tom b ni jijkn
int m=r.size(),n=b.size();
vector<vector<ll>> dp(n+10, vector<ll> (m+10));
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
dp[i][j]=1e17;
}
}
dp[0][0]=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
dp[i][j]=min(dp[i][j],dp[i-1][j] + abs(r[j-1] - b[i-1]));
dp[i][j]=min(dp[i][j],dp[i-1][j-1] + abs(r[j-1] - b[i-1]));
dp[i][j]=min(dp[i][j],dp[i][j-1] + abs(r[j-1] - b[i-1]));
}
}
return dp[n][m];
}