//tempaa
//#pragma GCC optimize ("-O3")
#ifdef _DEBUG
#include<cassert>
#include "bits_stdc++.h"
#else
#include <bits/stdc++.h>
#endif

#define r_ return
#define v_ vector
#define t_T template<class T>

using namespace std;
//@起動時
struct initon {
    initon() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout.setf(ios::fixed);
        cout.precision(3);
        srand((unsigned) clock() + (unsigned) time(NULL));
    };
} __initon;
//衝突対策
#define ws ___ws
#define over4(o1, o2, o3, o4, name, ...) name

#define int long long
#define ll long long
#define double long double
#define ull unsigned long long
using dou = double;
using itn = int;
using str = string;
using bo= bool;
#define au auto
using P = pair<ll, ll>;

#define fi first
#define se second
#define vec v_
#define beg begin
#define rbeg rbegin
#define con continue
#define bre break
#define brk break
#define is ==
using vi = v_<ll>;
using vb = v_<bool>;
using vs = v_<string>;
using vd = v_<double>;
using vc = v_<char>;
using vp = v_<P>;

//#define V v_
#define vvt0(t) v_<v_<t>>
#define vvt1(t, a) v_<v_<t>>a
#define vvt2(t, a, b) v_<v_<t>>a(b)
#define vvt3(t, a, b, c) v_<v_<t>> a(b,v_<t>(c))
#define vvt4(t, a, b, c, d) v_<v_<t>> a(b,v_<t>(c,d))

#define vvi(...) over4(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(ll,__VA_ARGS__)
#define vvb(...) over4(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(bool,__VA_ARGS__)
#define vvs(...) over4(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(string,__VA_ARGS__)
#define vvd(...) over4(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(double,__VA_ARGS__)
#define vvc(...) over4(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(char,__VA_ARGS__)
#define vvp(...) over4(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(P,__VA_ARGS__)


#define v3i(a, b, c, d) v_<v_<vi>> a(b, v_<vi>(c, vi(d)))
#define v3d(a, b, c, d) v_<v_<vd>> a(b, v_<vd>(c, vd(d)))
#define v3m(a, b, c, d) v_<v_<vm>> a(b, v_<vm>(c, vm(d)))


#define PQ priority_queue<ll, v_<ll>, greater<ll> >
#define tos to_string

using mapi = map<int, int>;
using mapd = map<dou, int>;
using mapc = map<char, int>;
using maps = map<str, int>;
using seti = set<int>;
using setd = set<dou>;
using setc = set<char>;
using sets = set<str>;
using qui = queue<int>;
#define bset bitset
#define uset unordered_set
#define mset multiset
#define umap unordered_map
#define umapi unordered_map<int,int>
#define umapp unordered_map<P,int>
#define mmap multimap

//マクロ 繰り返し
#define _overloadrep(_1, _2, _3, _4, name, ...) name
# define _rep(i, n) for(int i = 0,_lim=n; i < _lim ; i++)
#define repi(i, m, n) for(int i = m,_lim=n; i < _lim ; i++)
#define repadd(i, m, n, ad) for(int i = m,_lim=n; i < _lim ; i+= ad)
#define rep(...) _overloadrep(__VA_ARGS__,repadd,repi,_rep,)(__VA_ARGS__)
#define _rer(i, n) for(int i = n; i >= 0 ; i--)
#define reri(i, m, n) for(int i = m,_lim=n; i >= _lim ; i--)
#define rerdec(i, m, n, dec) for(int i = m,_lim=n; i >= _lim ; i-=dec)
#define rer(...) _overloadrep(__VA_ARGS__,rerdec,reri,_rer,)(__VA_ARGS__)
#define fora(a, b) for(auto&& a : b)

//マクロ 定数
#define k3 1010
#define k4 10101
#define k5 101010
#define k6 1010101
#define k7 10101010

t_T T MAX() { r_ numeric_limits<T>::max(); }
t_T T MIN() { r_ numeric_limits<T>::min(); }
const int inf = (int) 1e9 + 100;
const ll linf = (ll) 1e18 + 100;
t_T T INF() { r_ MAX<T>() / 2; }
template<> signed INF() { r_ inf; }
template<> ll INF() { r_ linf; }
template<> double INF() { r_ (double) linf * linf; }

const double eps = 1e-9;
const double PI = 3.1415926535897932384626433832795029L;
ll ma = numeric_limits<ll>::min();
ll mi = numeric_limits<ll>::max();
const int y4[] = {-1, 1, 0, 0};
const int x4[] = {0, 0, -1, 1};
const int y8[] = {0, 1, 0, -1, -1, 1, 1, -1};
const int x8[] = {1, 0, -1, 0, 1, -1, 1, -1};

//マクロ省略形 関数等
#define sz(a) ((int)(a).size())
#define rs resize
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()

/*@formatter:off*/
inline void sort(string &a) { sort(all(a)); }
t_T inline void sort(v_<T> &a) { sort(all(a)); };
t_T inline void rsort(v_<T> &a) { sort(all(a), greater<T>()); };
template<class U> inline void sortp(v_<U> &a, v_<U> &b) {    vp c;    int n = sz(a);    assert(n == sz(b));    rep(i, n)c.eb(a[i], b[i]);    sort(c);    rep(i, n) {        a[i] = c[i].first;        b[i] = c[i].second;;    }};//F = T<T>
//例えばr_ p.fi + p.se;
template<class U> inline void rsortp(v_<U> &a, v_<U> &b) {    vp c;    int n = sz(a);    assert(n == sz(b));    rep(i, n)c.eb(a[i], b[i]);    rsort(c);    rep(i, n) {        a[i] = c[i].first;        b[i] = c[i].second;    }};
template<typename V, typename T>
void fill(V &xx, const T vall) {xx = vall;}
template<typename V, typename T>
void fill(v_<V> &vecc, const T vall) {for (auto &&vx: vecc) fill(vx, vall);}

//@汎用便利関数 入力
template<typename T = int> T _in() {T x;cin >> x;r_ (x);}
#define _overloadin(_1, _2, _3, _4, name, ...) name
#define in0() _in()
#define in1(a) cin>>a
#define in2(a, b) cin>>a>>b
#define in3(a, b, c) cin>>a>>b>>c
#define in4(a, b, c, d) cin>>a>>b>>c>>d
#define in(...) _overloadin(__VA_ARGS__,in4,in3,in2 ,in1,in0)(__VA_ARGS__)

#define _overloaddin(_1, _2, _3, _4, name, ...) name
#define din1(a) int a;cin>>a
#define din2(a, b) int a,b;cin>>a>>b
#define din3(a, b, c) int a,b,c;cin>>a>>b>>c
#define din4(a, b, c, d) int a,b,c,d;cin>>a>>b>>c>>d
#define din(...) _overloadin(__VA_ARGS__,din4,din3,din2 ,din1)(__VA_ARGS__)

#define _overloaddind(_1, _2, _3, _4, name, ...) name
#define din1d(a) int a;cin>>a;a--
#define din2d(a, b) int a,b;cin>>a>>b;a--,b--
#define din3d(a, b, c) int a,b,c;cin>>a>>b>>c;a--,b--,c--
#define din4d(a, b, c, d) int a,b,c,d;cin>>a>>b>>c>>d;;a--,b--,c--,d--
#define dind(...) _overloaddind(__VA_ARGS__,din4d,din3d,din2d ,din1d)(__VA_ARGS__)
template<typename W, typename H> void resize(W &vec_, const H head) { vec_.resize(head); }
template<typename W, typename H, typename ... T> void resize(W &vec_, const H &head, const T ... tail) {vec_.resize(head);for (auto &v: vec_)resize(v, tail...);}


string sin() { r_ _in<string>(); }
ll lin() { r_ _in<ll>(); }
t_T void na(v_<T> &a, ll n) {a.resize(n);rep(i, n)cin >> a[i];}
#define dna(a, n) vi a; na(a, n);/*nを複数使うと n==in()の時バグる事に注意*/
#define dnad(a, n) vi a; nad(a, n);
t_T void nad(v_<T> &a, ll n) {a.resize(n);rep(i, n)cin >> a[i], a[i]--;}
template<class T, class U> void na2(v_<T> &a, v_<U> &b, ll n) {a.resize(n);b.resize(n);rep(i, n)cin >> a[i] >> b[i];}
#define dna2(a, b, n) vi a,b; na2(a,b,n);
template<class T, class U, class W> void na3(v_<T> &a, v_<U> &b, v_<W> &c, ll n) {a.resize(n);b.resize(n);c.resize(n);rep(i, n)cin >> a[i] >> b[i] >> c[i];}
#define nt(a, h, w) resize(a,h,w);rep(nthi,h)rep(ntwi,w) cin >> a[nthi][ntwi];
#define ntd(a, h, w) resize(a,h,w);rep(ntdhi,h)rep(ntdwi,w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--;
#define ntp(a, h, w) resize(a,h+2,w+2);fill(a,'#');rep(ntphi,1,h+1)rep(ntpwi,1,w+1) cin >> a[ntphi][ntpwi];
#define dnt(S, h, w) vvi(S,h,w);nt(S,h,w);
#define dntc(S, h, w) vvc(S,h,w);nt(S,h,w);
#define dnts(S, h, w) vvs(S,h,w);nt(S,h,w);

//デバッグ
#define sp << " " <<
constexpr int DEB_LEN =20;
constexpr int DEB_LEN_H =12;
string deb_tos(const int &v) { if (abs(v) == inf || abs(v) == linf)return "e"; else return to_string(v); }
template<class T> string deb_tos(const T &a) {stringstream ss;ss << a;return ss.str();}
template<class T> string deb_tos(const vector<T> &a, ll W = inf) {    stringstream ss;    if (W == inf)W = min(sz(a), DEB_LEN);    if (sz(a) == 0)return ss.str();    rep(i, W) {        ss << deb_tos(a[i]);        if (typeid(a[i]) == typeid(P)) { ss << endl; } else { ss << " "; }    }    return ss.str();}
template<class T> string deb_tos(const vector<vector<T> > &a, vi H, vi W, int key = -1) {    stringstream ss;    ss << endl;    vi lens(sz(W));    fora(h, H) {        rep(wi, sz(W)) {            lens[wi] = max(lens[wi], sz(deb_tos(a[h][W[wi]])) + 1);            lens[wi] = max(lens[wi], sz(deb_tos(W[wi])) + 1);        }    }    if (key == -1)ss << " *|"; else ss << " " << key << "|";    int wi = 0;    fora(w, W) {        ss << std::right << std::setw(lens[wi]) << w;        wi++;    }    ss << "" << endl;    rep(i, sz(W))rep(_, lens[i]) ss << "_";    rep(i, 3)ss << "_";    ss << "" << endl;    fora(h, H) {        ss << std::right << std::setw(2) << h << "|";        int wi = 0;        fora(w, W) {            ss << std::right << std::setw(lens[wi]) << deb_tos(a[h][w]);            wi++;        }        ss << "" << endl;    }    return ss.str();}
template<class T> string deb_tos(const vector<vector<T> > &a, ll H = inf, ll W = inf, int key = -1) {    H = (H != inf) ? H : min({H, sz(a), DEB_LEN_H});    W = min({W, sz(a[0]), DEB_LEN_H});    vi hs, ws;    rep(h, H) { hs.push_back(h); }    rep(w, W) { ws.push_back(w); }    return deb_tos(a, hs, ws, key);}
template<class T> string deb_tos(const vector<vector<vector<T> > > &a, ll H = inf) {    stringstream ss;    if (H == inf)H = DEB_LEN_H;    H = min(H, sz(a));    rep(i, H) {        ss << endl;        ss << deb_tos(a[i], inf, inf, i);    }    return ss.str();}
template<class T, size_t A> string deb_tos(T (&a)[A]) { return deb_tos(vector<T>(begin(a), end(a))); }
template<class T, size_t A, size_t B> string deb_tos(T (&a)[A][B]) { return deb_tos(vector<vector<T> >(begin(a), end(a))); }
template<class T, size_t A, size_t B, size_t C> string deb_tos(T (&a)[A][B][C]) { return deb_tos(vector<vector<vector<T> > >(begin(a), end(a))); }

#define debugName(VariableName) # VariableName

#define _deb1(x) cerr <<  debugName(x)<<" = "<<deb_tos(x) << endl
#define _deb2(x, y) cerr <<  debugName(x)<<" = "<<deb_tos(x)<<", "<< debugName(y)<<" = "<<deb_tos(y)<< endl
#define _deb3(x, y, z) cerr <<  debugName(x)<<" = "<<deb_tos(x)  << ", " <<  debugName(y)<<" = "<<deb_tos(y) <<", " debugName(z)<<" = "<<deb_tos(z) <<endl
#define _deb4(x, y, z, a) cerr <<  debugName(x)<<" = "<<deb_tos(x) <<", " <<   debugName(y)<<" = "<<deb_tos(y) <<", " <<  debugName(z)<<" = "<<deb_tos(z) <<", " <<  debugName(a)<<" = "<<deb_tos(a)<<endl
#define _deb5(x, y, z, a, b) cerr <<  debugName(x)<<" = "<<deb_tos(x) <<", " <<   debugName(y)<<" = "<<deb_tos(y) <<", " <<  debugName(z)<<" = "<<deb_tos(z) <<", " <<  debugName(a)<<" = "<<deb_tos(a)<<", " <<  debugName(b)<<" = "<<deb_tos(b)<<endl


#define _overloadebug(_1, _2, _3, _4, _5, name, ...) name
#define debug(...) _overloadebug(__VA_ARGS__,_deb5,_deb4,_deb3,_deb2,_deb1)(__VA_ARGS__)
#ifdef _DEBUG
#define deb(...) _overloadebug(__VA_ARGS__,_deb5,_deb4,_deb3,_deb2,_deb1)(__VA_ARGS__)
#else
#define deb(...) ;
#endif


#define debugline(x) cerr << x << " " << "(L:" << __LINE__ << ")" << '\n'


ll gcd(ll a, ll b) { r_ b ? gcd(b, a % b) : a; }
ll gcd(vi b) {ll res = b[0];for (auto &&v :b)res = gcd(v, res);r_ res;}
ll lcm(ll a, ll b) { r_ a / gcd(a, b) * b; }
ll rev(ll a) {    ll res = 0;    while (a) {        res *= 10;        res += a % 10;        a /= 10;    }    r_ res;}
t_T v_<T> rev(v_<T> &a) {    auto b = a;    reverse(all(b));    r_ b;}
str rev(str &a) {str s = a;reverse(all(s));r_ s;}
ll ceil(ll a, ll b) {if (b == 0) {cerr<<"ceil"<<endl;exit(1);r_ -1;} else r_ (a + b - 1) / b;}
ll sqrt(ll a) {    if (a < 0) {        cerr<<"sqrt"<<endl;        exit(1);    }    ll res = (ll) std::sqrt(a);    while (res * res < a)res++;    r_ res;}
double log(double e, double x) { r_ log(x) / log(e); }
ll sig(ll t) { r_ (1 + t) * t / 2; }
ll sig(ll s, ll t) { r_ (s + t) * (t - s + 1) / 2; }

vi divisors(int v) {    vi res;    double lim = std::sqrt(v);    rep(i, 1, lim+1){        if (v % i == 0) {            res.pb(i);            if (i != v / i)res.pb(v / i);        }    }    r_ res;}
vb isPrime;
vi primes;

void setPrime() {    int len = 4010101;    isPrime.resize(4010101);    fill(isPrime, true);    isPrime[0] = isPrime[1] = false;    for (int i = 2; i <= sqrt(len) + 5; ++i) {        if (!isPrime[i])continue;        for (int j = 2; i * j < len; ++j) {            isPrime[i * j] = false;        }    }    rep(i, len)if (isPrime[i])primes.pb(i);}

vi factorization(int v) {    int tv = v;    vi res;    if (isPrime.size() == 0)setPrime();    for (auto &&p :primes) {        if (v % p == 0)res.push_back(p);        while (v % p == 0) {            v /= p;        }        if (v == 1 || p * p > tv)break;    }    if (v > 1)res.pb(v);    r_ res;}
inline bool inside(int h, int w, int H, int W) { r_ h >= 0 && w >= 0 && h < H && w < W; }
inline bool inside(int v, int l, int r) { r_ l <= v && v < r; }


t_T v_<T> ruiv(v_<T> &a) {    v_<T> ret(a.size() + 1);    rep(i, a.size())ret[i + 1] = ret[i] + a[i];    r_ ret;}
//imoは0-indexed
//ruiは1-indexed
t_T v_<T> imo(v_<T> &v) {    v_<T> ret = v;    rep(i, sz(ret) - 1)ret[i + 1] += ret[i];    r_ ret;}

#define ins inside
ll u0(ll a) { r_ a < 0 ? 0 : a; }
t_T v_<T> u(const v_<T> &a) {
    v_<T> ret = a;fora(v, ret)v = u0(v);r_ ret;}
#define MIN(a) numeric_limits<a>::min()
#define MAX(a) numeric_limits<a>::max()
template<class T, class U> pair<T,U> operator+(const pair<T,U> &a, const pair<T,U> & b) {return pair<T,U>(a.fi+b.fi,a.se+b.se);}

void yn(bool a) {
    if (a)cout << "yes" << endl;
    else cout << "no" << endl;
}
void Yn(bool a) {
    if (a)cout << "Yes" << endl;
    else cout << "No" << endl;
}
void YN(bool a) {
    if (a)cout << "YES" << endl;
    else cout << "NO" << endl;
}
void fyn(bool a) {
    if (a)cout << "yes" << endl;
    else cout << "no" << endl;
    exit(0);
}
void fYn(bool a) {
    if (a)cout << "Yes" << endl;
    else cout << "No" << endl;
    exit(0);
}
void fYN(bool a) {
    if (a)cout << "YES" << endl;
    else cout << "NO" << endl;
    exit(0);
}
void Possible(bool a) {
    if (a)cout << "Possible" << endl;
    else cout << "Impossible" << endl;
    exit(0);
}
void POSSIBLE(bool a) {
    if (a)cout << "POSSIBLE" << endl;
    else cout << "IMPOSSIBLE" << endl;
    exit(0);
}
template<class T, class U> set<T> &operator+=(set<T> &a, U v) {
    a.insert(v);
    r_ a;
}
template<class T, class U> v_<T> &operator+=(v_<T> &a, U v) {
    a.push_back(v);
    r_ a;
}
template<class T> v_<T>& operator+=(v_<T> & a,const v_<T> & b){
    fora(v, b)a += v;
    return a;
}
t_T T sum(v_<T> &v, int s = 0, int t = inf) {    T ret = 0;    rep(i, s, min(sz(v), t))ret += v[i];    r_ ret;}
void mod(int &a, int m) { a = (a % m + m) % m; }
template<class F> inline int mgr(int ok, int ng, F f) {
#define _mgrbody int mid = (ok + ng) / 2; if (f(mid))ok = mid; else ng = mid;
    if (ok < ng)while (ng - ok > 1) { _mgrbody } else while (ok - ng > 1) { _mgrbody }
    r_ ok;
}

template<class F> inline int mgr(int ok, int ng, int second, F f) {
#define _mgrbody2 int mid = (ok + ng) / 2; if (f(mid, second))ok = mid; else ng = mid;
    if (ok < ng) while (ng - ok > 1) { _mgrbody2 } else while (ok - ng > 1) { _mgrbody2 }
    r_ ok;
}
template<typename T> ostream &operator<<(ostream &os, v_<T> &m) {    rep(i, sz(m)){os << m[i];if(i < sz(m)-1)os<<" ";}   r_ os;}
ostream &operator<<(ostream &os, v_<char> &m) {rep(i, sz(m)){os << m[i];}   r_ os;}
template<typename T> ostream &operator<<(ostream &os, v_<v_<T>> &m) {rep(i, sz(m)){os << m[i]; if(i < sz(m)-1)os << endl;}   r_ os;}
constexpr bool bget(ll m, int keta) { r_ (m >> keta) & 1; }
int bget(ll m, int keta, int sinsuu) {m /= (ll) pow(sinsuu, keta);r_ m % sinsuu;}
ll bit(int n) { r_ (1LL << (n)); }
ll bit(int n, int sinsuu) { r_ (ll) pow(sinsuu, n); }
int mask(int n) { r_ (1ll << n) - 1; }
#define bcou __builtin_popcountll
template<class T, class U> inline bool chma(T &a, const U &b) {if (a < b) {a = b;r_ true;}r_ false;}
template<class U> inline bool chma(const U &b) { r_ chma(ma, b); }
template<class T, class U> inline bool chmi(T &a, const U &b) {    if (b < a) {        a = b;        r_ true;    }    r_ false;}
template<class U> inline bool chmi(const U &b) { r_ chmi(mi, b); }
#define unique(v) v.erase( unique(v.begin(), v.end()), v.end() );
int max(vi &a) {    int res = a[0];    fora(v, a) {        res = max(res, v);    }    r_ res;}
int min(vi &a) {    int res = a[0];    fora(v, a) {        res = min(res, v);    }    r_ res;}
template<typename T> class fixed_point        : T {public:    explicit constexpr fixed_point(T &&t) noexcept: T(std::forward<T>(t)) {}    template<typename... Args> constexpr decltype(auto) operator()(Args &&... args) const { r_ T::operator()(*this, std::forward<Args>(args)...); }};template<typename T> static inline constexpr decltype(auto) fix(T &&t) noexcept { r_ fixed_point<T>{std::forward<T>(t)}; }
constexpr ll h4[] = {1, -1, 0, 0};
constexpr ll w4[] = {0, 0, -1, 1};
constexpr ll h8[] = {0, 1, 0, -1, -1, 1, 1, -1};
constexpr ll w8[] = {1, 0, -1, 0, 1, -1, 1, -1};
int mei_inc(int h, int w, int H, int W, int i) {while (++i < 4) { if (inside(h + h4[i], w + w4[i], H, W))r_ i; }r_ i;}
#define mei(nh, nw, h, w) for (int i = mei_inc(h, w, H, W, -1), nh = i<4? h + h4[i] : 0, nw = i<4? w + w4[i] : 0; i < 4; i=mei_inc(h,w,H,W,i), nh = h+h4[i], nw = w+w4[i])
int mei_inc8(int h, int w, int H, int W, int i) {    while (++i < 8) {        if (inside(h + h8[i], w + w8[i], H, W))r_ i;    }    r_ i;}
#define mei8(nh, nw, h, w) for (int i = mei_inc8(h, w, H, W, -1), nh = i<8? h + h8[i] : 0, nw = i<8? w + w8[i] : 0; i < 8; i=mei_inc8(h,w,H,W,i), nh = h+h8[i], nw = w+w8[i])
int mei_incv(int h, int w, int H, int W, int i, vp &p) {    while (++i < sz(p)) { if (inside(h + p[i].fi, w + p[i].se, H, W))r_ i; }    r_ i;}
#define meiv(nh, nw, h, w, p) for (int i = mei_incv(h, w, H, W, -1, p), nh = i<sz(p)? h + p[i].fi : 0, nw = i<sz(p)? w + p[i].se : 0; i < sz(p); i=mei_incv(h,w,H,W,i,p), nh = h+p[i].fi, nw = w+p[i].se)
vi compress(vi &A) {    vi B = A;    sort(B);    unique(B);    vi inds(sz(B));    rep(i, sz(A)) {        int ind = lower_bound(all(B), A[i]) - B.begin();        inds[ind] = A[i];        A[i] = ind;    }    return inds;}
vi compress(vi &A, umapi& map){    vi i_v = compress(A);    rep(i, sz(i_v)){        map[i_v[i]] = i;    }    return i_v;}

t_T void out2(T head) {    cout << head;}
template<class T, class... U> void out2(T head, U ... tail) {    cout << head << " ";      out2(tail...);}
template<class T, class... U> void out(T head, U ... tail) {    cout << head << " ";        out2(tail...);    cout << "" << endl;}
t_T void out(T head) {    cout << head << endl; }
void out() { cout << "" << endl; }
#define lowerIndex(a, v) (lower_bound(all(a),v)-a.begin())
#define upperIndex(a, v) (upper_bound(all(a),v)-a.begin())
template<typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); }
#define vni(name, ...) auto name = make_v<ll>(__VA_ARGS__)
#define vnb(name, ...) auto name = make_v<bool>(__VA_ARGS__)
#define vns(name, ...) auto name = make_v<string>(__VA_ARGS__)
#define vnd(name, ...) auto name = make_v<double>(__VA_ARGS__)
#define vnc(name, ...) auto name = make_v<char>(__VA_ARGS__)
#define vnp(name, ...) auto name = make_v<P>(__VA_ARGS__)
#define vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__)
#define VEC vector

/*@formatter:on*/
int N, M, H, W;
vi A, B, C;

/*@formatter:off*/
//整数用幾何ライブラリ   *@formatter:off*/
vp ort_next_ = {{1, 0}, {0,  1}, {-1, 0}, {0,  -1}, {1,  0}};
vp ort_prev_ = {{1, 0}, {1,  0}, {0, 1}, {-1,  0}, {0,  -1}};
class D {
public:
    int x, y;
    D(): x(0), y(0) {}
    D(int x, int y): x(x), y(y) {}
    D(P p): x(p.first), y(p.second){}
    D& operator+=( D& v) { x += v.x; y += v.y; return *this;}
    D operator+( D& v)  { return D(*this) += v;}
    D& operator-=( D& v) { x -= v.x; y -= v.y; return *this;}
    D operator-( D& v)  { return D(*this) -= v;}
    D& operator*=(int s) { x *= s; y *= s; return *this;}
    D operator*(int s)  { return D(*this) *= s;}
    D& operator/=(int s) { x /= s; y /= s; return *this;}
    D operator/(int s)  { return D(*this) /= s;}
    bool operator==(D & v) {return x == v.x && y == v.y;}
    bool operator!=(D & v) {return !operator==(v);}
    //内積
    int dot( D& v)  { return x*v.x + y*v.y;}
    //外積
    int cross( D& v)  { return x*v.y - v.x*y;}
    int cross( int rx,int ry)  { return x*ry - rx*y;}
    //距離の2乗
    int norm2()  { return x*x + y*y;}
    double norm()  { return sqrt(norm2());}
    //単位ベクトルを返す
    D unit(){return (*this)/ norm();}
    // 象限
    int ort()  {        if (abs(x) < eps && abs(y) <= eps) return 0;        if (y > 0) return x>0 ? 1 : 2;        else return x<0 ? 3 : 4;    }
    int ort(int x,int y)  {        if (abs(x) < eps && abs(y) <= eps) return 0;        if (y > 0) return x>0 ? 1 : 2;        else return x<0 ? 3 : 4;    }
    //偏角でソート出来る
    bool operator<(const D &v) const {return x == v.x ? y < v.y : x < v.x;}
    bool operator>(D &v) {return x == v.x ? y > v.y : x > v.x;}
    /*@formatter:off*/
    //反時計回りで隣接する象限との境界にある、長さ1の点を返す
    //(+,+)で呼ぶと(0, 1)が返る
    D ort_bound_next(){        int i=ort();        assert(i);        return D(ort_next_[i]);    }
    //時計回り
    D ort_bound_prev(){        int i=ort();        assert(i);        return D(ort_prev_[i]);    }
};
//反時計回り
class rot {//@formatter:off
    vector<vc> pre;//今が斜めでないときは使えない
    int preAdd=0;    void rot90() {        preAdd += 2;        swap(H, W);        vvc(nv, H, W);        rep(fh, W) {            rep(fw, H) {                int th = H - fw - 1;                int tw = fh;                nv[th][tw] = v[fh][fw];            }        }        swap(v, nv);    }    void rot180() {        preAdd += 4;        vvc(nv, H, W);        rep(fh, H) {            rep(fw, W) {                int th = H - fh - 1;                int tw = W - fw - 1;                nv[th][tw] = v[fh][fw];            }        }        swap(v, nv);    }    void rot270() {        preAdd += 6;        swap(H, W);        vvc(nv, H, W);        rep(fh, W) {            rep(fw, H) {                int th = fw;                int tw = W - 1 - fh;                nv[th][tw] = v[fh][fw];            }        }        swap(v, nv);    }
public:
    vector<vc> v;
    int H, W;
    bool naname = 0;
    rot(vector<vc> &b) : v(b), H(sz(b)), W(sz(b[0])) {}
    P rot45(int h, int w) { return P(h - w + W - 1, h + w); }
    P rot45_rev(int h, int w) { return P(h + w, -h + w + H - 1); }
    rot &operator++() {        /*preを90度回転させる*/        if (naname) {            H = sz(pre[0]);            W = sz(pre);            vvc(nv, H, W);            rep(fh, W) {                rep(fw, H) {                    int th = H - fw - 1;                    int tw = fh;                    nv[th][tw] = pre[fh][fw];                }            }            swap(pre, v);            swap(v, nv);            naname ^= 1;            operator+=(preAdd);            preAdd = 0;            return *this;        } else {            int NH = H + W - 1;            int NW = H + W - 1;            vvc(a, NH, NW);            /*h += Wとする*/            rep(h, H) {                rep(w, W) {                    int nh, nw;                    tie(nh, nw) = rot45(h, w);                    a[nh][nw] = v[h][w];                }            }            swap(a, v);            swap(pre, a);            H = NH;            W = NW;            naname ^= 1;            return *this;        }    }
    rot operator++(signed) {        rot ret = *this;        operator++();        return ret;    }
    rot operator+(int k) {        rot ret = *this;        ret.operator+=(k);        return ret;    }
    void operator+=(int k) {        k %= 8;        if (k >= 6) {            rot270();            k -= 6;        } else if (k >= 4) {            rot180();            k -= 4;        } else if (k >= 2) {            rot90();            k -= 2;        }        if (k)operator++();    }
    /*反時計回りで統一しないと、preがおかしくなる*/
    /*preを廃止して遷移するのは実装がつらい上に見返りが少なそう*/
    /*必要になったら作る*/
    rot &operator--() {        rot270();        return operator++();    }
    rot operator--(signed) {        rot ret = *this;        operator--();        return ret;    }
    rot operator-(int k) {        rot ret = *this;        ret.operator-=(k);        return ret;    }
    void operator-=(int k) { operator+=(((-k % 8) + 8) % 8); }
    int size() { return H; }
    vector<vector<char>>& operator()(){return v;}
    vc &operator[](int i) { return v[i]; }
    operator vector<vc> &() { return v; }
};
ostream &operator<<(ostream &os, rot &v) {os << v.v;return os;}
using dots = vector<D>;
//線分
struct E { D f, t; E(D f = D(), D t = D()) : f(f), t(t) {}};
bool eq(dots &a, dots &b) {    if (sz(a) != sz(b))return false;    rep(i, sz(a)) { if (a[i] != b[i])return false; }    return true;}
//fを中心として、tを90*inc度 反時計回りに回転
D rot90(D &f, D &t, int inc) {    inc=((inc % 4)+4)%4;    D ret = t - f;    if (inc < 0) {        while (inc) {            ret = D(ret.y, -ret.x);            inc++;        }    } else if (inc > 0) {        while (inc > 0) {            ret = D(-ret.y, ret.x);            inc--;        }    }    return f + ret;}
dots rot90(D &f, dots &t, int inc) {    int N = sz(t);    dots res(N);    rep(i, N) { res[i] = rot90(f, t[i], inc); }    return res;}
istream &operator>>(istream &ist, D &v) {    ist >> v.x >> v.y;    return ist;}ostream &operator<<(ostream &os, D &v) {    os << "(" << v.x << "," << v.y << ")";    return os;}
vector<D> operator-=(dots &a, D &b) {    fora(v, a) { v -= b; }    return a;}
vector<D> operator-(dots &a, D &b) {    dots c(sz(a));    int i = 0;    fora(v, a) { c[i++] = v - b; }    return c;}
/*@formatter:on*/
P rot90(const P &p, int inc) {
    P now = p;
    inc = ((inc % 4) + 4) % 4;
    auto rot = [&](P &now) {
        int y, x;
        tie(y, x) = now;
        now = P(x, -y);
    };
    while (inc--)rot(now);
    return now;
}
/*@formatter:off*/
//Pはh, wで渡す その他は不明

/*@formatter:on*/
//反時計回り
//y軸からx軸にかけての回転なので、右下へ向かう場合は時計回りになる事に注意
void solve() {
    //現在の方向p, dをマージ
    while (cin >> W >> H, H && W) {
        vvi(S);
        nt(S, H, W);
        auto mix = [&](P p, int d) {
            if (d == 0)return p;
            if (d == 1)return rot90(p, 1);
            if (d == 2)return rot90(p, 2);
            if (d == 3)return rot90(p, 3);
            if (d == 4)return P(0, 0);
        };

        vi C;
        na(C, 4);
        C += linf;
        VEC<VEC<map<P, int>>> dp;
        resize(dp, H, W);
        rep(h, H)
            rep(w, W) {
                dp[h][w][mp(0, 0)] = linf;
                dp[h][w][mp(0, 1)] = linf;
                dp[h][w][mp(0, -1)] = linf;
                dp[h][w][mp(1, 0)] = linf;
                dp[h][w][mp(-1, 0)] = linf;
            }
        dp[0][0][mp(0, 1)] = 0;
        using T =tuple<int, int, int, P>;
        priority_queue<T, vector<T>, greater<T> > q;//小さい順
        int cost = linf;
        q.push(mt(0, 0, 0, mp(0, 1)));
        while (sz(q)) {
            int c;
            int h, w;
            P d;
            tie(c, h, w, d) = q.top();
            q.pop();
            if (cost < c)continue;
            vi C2 = C;
            C2[S[h][w]] = 0;
            rep(adi, 4) {
                P nd = mix(d, adi);
                int nc = c + C2[adi];
                int nh, nw;
                tie(nh, nw) = mp(h, w) + nd;
                if (!inside(nh, nw, H, W))continue;
                if (nh == h && nw == w)continue;
                if (chmi(dp[nh][nw][nd], nc)) {
                    if (nh == H - 1 && nw == W - 1) {
                        chmi(cost, nc);
                    }
                    q.push(mt(nc, nh, nw, nd));
                }
            }
        }
        int mi = linf;
        chmi(mi, dp[H - 1][W - 1][mp(0, 0)]);
        chmi(mi, dp[H - 1][W - 1][mp(0, 1)]);
        chmi(mi, dp[H - 1][W - 1][mp(0, -1)]);
        chmi(mi, dp[H - 1][W - 1][mp(1, 0)]);
        chmi(mi, dp[H - 1][W - 1][mp(-1, 0)]);
        out(mi);
    }
}

signed main() {
    solve();
    r_ 0;
}



Measured memory_bytes: 3572