const INF: usize = 1usize << 60; fn main() { let mut nm = String::new(); std::io::stdin().read_line(&mut nm).ok(); let nm: Vec = nm.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = nm[0]; let m = nm[1]; let mut a = String::new(); std::io::stdin().read_line(&mut a).ok(); let a: Vec = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut b = String::new(); std::io::stdin().read_line(&mut b).ok(); let b: Vec = b.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let a = (0..n).flat_map(|i| { let mut ret = vec![1usize; a[i]+1]; ret[0] = 0; ret }) .collect::>(); let b = (0..m).flat_map(|i| { let mut ret = vec![1usize; b[i]+1]; ret[0] = 0; ret }) .collect::>(); let n = a.len(); let m = b.len(); let mut dp = vec![vec![INF; m+1]; n+1]; dp[0][0] = 0; for i in 0..=n { for j in 0..=m { if i+1 <= n { dp[i+1][j] = dp[i+1][j].min(dp[i][j] + 1); } if j+1 <= m { dp[i][j+1] = dp[i][j+1].min(dp[i][j] + 1); } if i+1 <= n && j+1 <= m { dp[i+1][j+1] = dp[i+1][j+1].min(dp[i][j] + if a[i] == b[j] { 0 } else { 1 }); } } } println!("{}", dp[n][m]); }