use proconio::input; const INF: usize = 10_usize.pow(6); fn main() { input! { (n, q): (usize, usize), aa: [usize; n], bb: [usize; q], } let mut dp = vec![INF; n]; dp[0] = 0; for &b in &bb { let mut next_dp = vec![INF; n]; let mut to_right = INF; for (i, &a) in aa.iter().enumerate() { to_right = to_right.min(dp[i] + n - 1 - i); if a == b { next_dp[i] = next_dp[i].min(to_right - (n - 1 - i)); } } let mut to_left = INF; for (i, &a) in aa.iter().enumerate().rev() { to_left = to_left.min(dp[i] + i); if a == b { next_dp[i] = next_dp[i].min(to_left - i); } } dp = next_dp; } let ans = *dp.iter().min().unwrap(); println!("{}", ans); }