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![1; a[i]+1]; ret[0] = 0; ret }).collect::>(); let b = (0..m).flat_map(|i| { let mut ret = vec![1; b[i]+1]; ret[0] = 0; ret }).collect::>(); let mut dp = vec![vec![INF; b.len()+1]; a.len()+1]; dp[0][0] = 0; for i in 0..=a.len() { for j in 0..=b.len() { if i+1 <= a.len() { dp[i+1][j] = dp[i+1][j].min(dp[i][j] + 1); } if j+1 <= b.len() { dp[i][j+1] = dp[i][j+1].min(dp[i][j] + 1); } if i+1 <= a.len() && j+1 <= b.len() { dp[i+1][j+1] = dp[i+1][j+1].min(dp[i][j] + if a[i] == b[j] { 0 } else { 1 }); } } } println!("{}", dp[a.len()][b.len()]); }