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 rooms = (0..n).map(|_| { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); temp }) .collect::>(); if n == 1 { println!("0"); return; } rooms.insert(0, vec![0usize; m]); let mut dp = vec![vec![INF; m]; n+1]; for i in 0..m { dp[0][i] = 0; } for i in 0..n { let minval = (0..m).map(|j| dp[i][j]).min().unwrap(); for j in 0..m { dp[i+1][j] = dp[i+1][j].min(dp[i][j] + rooms[i+1][j]); dp[i+1][j] = dp[i+1][j].min(minval + rooms[i][j] + rooms[i+1][j]); } } println!("{}", dp[n].iter().min().unwrap()); }