extern crate core;

use std::fmt;
use std::cmp::{Ordering, min, max};
use std::f32::MAX;
use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut, SubAssign};
use std::collections::{BTreeMap, VecDeque, BinaryHeap, BTreeSet};
use std::fmt::{Display, Formatter, Error};

fn show<T: Display>(vec: &Vec<T>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        print!("[{}", vec[0]);
        for i in 1 .. vec.len() {
            print!(", {}", vec[i]);
        }
        println!("]");
    }
}
fn show2<T: Display>(vec: &Vec<Vec<T>>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        for l in vec {
            show(l);
        }
    }
}
macro_rules! read_line{
    () => {{
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).ok();
        line
    }};
    (delimiter: ' ') => {
        read_line!().split_whitespace().map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (delimiter: $p:expr) => {
        read_line!().split($p).map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (' ') => {
        read_line!(delimiter: ' ')
    };
    ($delimiter:expr) => {
        read_line!(delimiter: $delimiter)
    };
    (' '; $ty:ty) => {
        read_line!().split_whitespace().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
    ($delimiter:expr; $ty:ty) => {
        read_line!($delimiter).into_iter().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
}
macro_rules! read_value{
    () => {
        read_line!().trim().parse().ok().unwrap()
    }
}
macro_rules! let_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
macro_rules! let_mut_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let mut $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
#[derive(Copy, Clone)]
pub struct Dice {
    top: usize, south: usize, east: usize
}
impl Default for Dice {
    fn default() -> Self {
        Dice{ top: 1, south: 2, east: 3}
    }
}
impl Dice {
    fn bottom(&self) -> i32 {
        7 - self.top as i32
    }
    fn rotate_north(&self) -> Dice {
        Dice{ top: self.south, south: 7 - self.top, east: self.east }
    }
    fn rotate_south(&self) -> Dice {
        Dice{ top: 7 - self.south, south: self.top, east: self.east }
    }
    fn rotate_east(&self) -> Dice {
        Dice{ top: 7 - self.east, south: self.south, east: self.top }
    }
    fn rotate_wast(&self) -> Dice {
        Dice{ top: self.east, south: self.south, east: 7 - self.top }
    }
}
struct ValueWithKey<V, K> {
    value: V, key: K
}
impl <V, K: Ord> Ord for ValueWithKey<V, K> {
    fn cmp(&self, other: &Self) -> Ordering {
        other.key.cmp(&self.key)
    }
}
impl <V, K: PartialOrd> PartialOrd for ValueWithKey<V, K> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        other.key.partial_cmp(&self.key)
    }
}
impl <V, K: PartialEq> PartialEq for ValueWithKey<V, K> {
    fn eq(&self, other: &Self) -> bool {
        other.key.eq(&self.key)
    }
}
impl <V, K: Eq> Eq for ValueWithKey<V, K> {}
struct Coordinate {
    x: usize, y: usize
}
fn main(){
    loop {
        let_all!(h: usize, w: usize);
        if h == 0 && w == 0 {
            return
        }
        let mut state: Vec<Vec<i32>> = Vec::with_capacity(h);
        for _ in 0 .. h {
            state.push(read_line!(' '; i32));
        }
        let mut memo = vec![vec![vec![vec![std::i32::MAX;7];7];w];h];
        let_all!(sy: usize, sx: usize);
        let_all!(gy: usize, gx: usize);
        let mut queue = BinaryHeap::new();
        memo[sy][sx][1][2] = 0;
        queue.push(ValueWithKey{value: (Dice::default(), Coordinate{x: sx, y: sy}), key: 0});
        while let Some(ValueWithKey{value: (dice, Coordinate{x:x, y:y}), key: cost}) = queue.pop() {
            if x == gx && y == gy {
                println!("{}", cost);
                break
            }
            if memo[y][x][dice.top][dice.south] == cost {
                if y > 0 {
                    let next = dice.rotate_north();
                    if memo[y - 1][x][next.top][next.south] > cost + state[y - 1][x] * next.bottom() {
                        memo[y - 1][x][next.top][next.south] = cost + state[y - 1][x] * next.bottom();
                        queue.push(ValueWithKey{value: (next, Coordinate{x: x, y: y - 1}), key: memo[y - 1][x][next.top][next.south]});
                    }
                }
                if y + 1 < h {
                   let next = dice.rotate_south();
                    if memo[y + 1][x][next.top][next.south] > cost + state[y + 1][x] * next.bottom() {
                        memo[y + 1][x][next.top][next.south] = cost + state[y + 1][x] * next.bottom();
                        queue.push(ValueWithKey{value: (next, Coordinate{x: x, y: y + 1}), key: memo[y + 1][x][next.top][next.south]});
                    }
                }
                if x > 0 {
                    let next = dice.rotate_wast();
                    if memo[y][x - 1][next.top][next.south] > cost + state[y][x - 1] * next.bottom() {
                        memo[y][x - 1][next.top][next.south] = cost + state[y][x - 1] * next.bottom();
                        queue.push(ValueWithKey{value: (next, Coordinate{x: x - 1, y: y}), key: memo[y][x - 1][next.top][next.south]});
                    }
                }
                if x + 1 < w {
                    let next = dice.rotate_east();
                    if memo[y][x + 1][next.top][next.south] > cost + state[y][x + 1] * next.bottom() {
                        memo[y][x + 1][next.top][next.south] = cost + state[y][x + 1] * next.bottom();
                        queue.push(ValueWithKey{value: (next, Coordinate{x: x + 1, y: y}), key: memo[y][x + 1][next.top][next.south]});
                    }
                }
            }
        }
    }
}


Measured memory_bytes: 5064