#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <deque>
#include <assert.h>
#include <bitset>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <tuple>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <math.h>
using namespace std;

inline int in(){int32_t x ; scanf("%d",&x) ; return x ; }
inline string get(){char ch[1001000] ; scanf("%s",ch) ; return ch;}
template<class P, class Q> inline P smin(P &a, Q b) { if (b < a) a = b; return a; }
template<class P, class Q> inline P smax(P &a, Q b) { if (a < b) a = b; return a; }
#define mp make_pair
#define EPS 1e-9
//#define int long long
typedef pair<int , int > pii;
typedef long double ld;
typedef unsigned long long ull;
typedef long long ll;
const int maxn = 1e5 + 10;
const int base = 29;
const int MAX_LG = 21;
const int mod = 1e9 + 7;
const int INF = 1e18;
int n , t;
string a[30] , b[30];
int dist1[30][30] , dist2[30][30];
int dx[] = {0,0,-1,1};
int dy[] = {1,-1,0,0};
struct edge
{
    int a , b , cap , flow;
};
int di[maxn];
int ptr[maxn];
int q[maxn];
vector<edge> e;
vector<int> g[maxn];
int whereX , whereY;
int source , sink;
inline void add(int a , int b , int cap)
{
    edge e1 = {a,b,cap,0};
    edge e2 = {b,a,0,0};
    g[a].push_back((int)e.size());
    e.push_back(e1);
    g[b].push_back((int)e.size());
    e.push_back(e2);
}
inline bool ok(int x , int y){return (x >= 0 && x < n && y >= 0 && y < n);}
inline void BFS1(int x = whereX , int y = whereY)
{
    queue<pii> q;
    q.push({x,y});
    for (int i = 0 ; i <= 20 ; i ++)
        for (int j = 0 ; j <= 20 ; j ++)
            dist1[i][j] = 1e9;
    dist1[x][y] = 0;
    while (!q.empty())
    {
        int x = q.front().first , y = q.front().second;
        q.pop();
        for (int i = 0 ; i < 4 ; i ++)
        {
            int x2 = x + dx[i] , y2 = y + dy[i];
            if (ok(x2,y2) && dist1[x2][y2] >= 1e9 && b[x2][y2] != 'Y')
            {
                dist1[x2][y2] = dist1[x][y] + 1;
                q.push({x2,y2});
            }
        }
    }

}
inline bool bfs()
{
    memset(di,-1,n * sizeof di[0]);
    int qh = 0 , qt = 0;
    q[qt ++] = source;
    di[source] = 0;
    while (qh < qt && di[sink] == -1)
    {
        int v = q[qh ++];
        for (int pt = 0 ; pt < g[v].size() ; pt ++)
        {
            int id = g[v][pt] , to = e[id].b;
            if (di[to] == -1 && e[id].flow < e[id].cap)
            {
                q[qt ++] = to;
                di[to] = di[v] + 1;
            }
        }
    }
    return di[sink] != -1;
}
inline int dfs(int v , int flow)
{
    if (!flow) return 0;
    if (v == sink) return flow;
    for ( ; ptr[v] < g[v].size() ; ptr[v] ++)
    {
        int id = g[v][ptr[v]] , to = e[id].b;
        if (di[to ] != di[v] + 1) continue;
        int pushed = dfs(to , min(flow , e[id].cap - e[id].flow));
        if (pushed)
        {
            e[id].flow += pushed;
            e[id^1].flow -= pushed;
            return pushed;
        }
    }
    return 0;
}
inline int max_flow()
{
    int flow = 0;
    while (true)
    {
        if (!bfs()) break;
        memset(ptr,0,n * sizeof ptr[0]);
        while (int pushed = dfs(source , 1e9))
        {
            flow += pushed;
        }
    }
    return flow;
}
inline void BFS2(int x , int y)
{
    queue<pii> q;
    q.push({x,y});
    for (int i = 0 ; i <= 20 ; i ++)
        for (int j = 0 ; j <= 20 ; j ++)
            dist2[i][j] = 1e9;
    dist2[x][y] = 0;
    while (!q.empty())
    {
        int x = q.front().first , y = q.front().second;
        q.pop();
        if (dist2[x][y] >= dist1[x][y]) continue;
        for (int i = 0 ; i < 4 ; i++)
        {
            int x2 = x + dx[i] , y2 = y + dy[i];
            if(ok(x2,y2) &&  dist2[x2][y2] >= 1e9 && a[x2][y2] != 'Y' && dist2[x][y] + 1 <= dist1[x2][y2])
            {
                dist2[x2][y2] = dist2[x][y] + 1;
                q.push({x2,y2});
            }
        }
    }
}
int32_t main()
{
    n = in() , t = in();
    for (int i = 0 ; i < n ; i ++)
        a[i] = get();
    for (int i = 0 ; i < n ; i ++)
    {
        b[i] = get();
        for (int j = 0 ; j < n ; j ++)
        {
            if (b[i][j] == 'Z')
            {
                b[i][j] = a[i][j] = 'Y';
                whereX = i , whereY = j;
            }
        }
    }
    BFS1();
    source = 0 , sink = 2 * n * n + 1;
    for (int i = 0 ; i < n ; i ++)
    {
        for (int j = 0 ; j < n ; j ++)
        {
            if (a[i][j] == 'Y') continue;
            int what = i * n + j + 1;
            int what2 = n * n + i * n + j + 1;
            add(source,what,a[i][j]-'0');
            add(what2,sink,b[i][j]-'0');
            BFS2(i , j);
            for (int ptx = 0 ; ptx < n ; ptx ++)
            {
                for (int pty = 0 ; pty < n ; pty ++)
                {
                    if (b[ptx][pty] == 'Y' || dist2[ptx][pty] > t) continue;
                    add(what,n * n + ptx * n + pty + 1 , a[i][j] - '0');
                }
            }
        }
    }
    n = 2 * n * n + 5;

//    printf("%d\n",max_flow());
    cout << max_flow() << endl;
}


#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <queue>

using namespace std;

struct node
{
  int r,c,dis;
};

const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
queue<node> q;
char s[15];
int n,n1,m,lim,tot,p,a[11][11],b[11][11],d0[11][11],f[202][202],d[202][202],w[202],h[202],g[203];
bool t,flag,ok[12][12],okb[12][12];

void search(int x,int z)
{
  if(x==n+1)
  {
    p=z;
    flag=true;
    return;
  }
  int temp=n+2;
  for(int y=0;y<=n+1;++y)
  {
    if(d[x][y]<f[x][y])
    {
      temp=min(temp,h[y]);
      if(h[x]==h[y]+1)
      {
        w[++tot]=y;
        search(y,min(z,f[x][y]-d[x][y]));
        if((!t)||(flag))
          return;
        w[tot--]=0;
      }
    }
    if(d[y][x]>0)
    {
      temp=min(temp,h[y]);
      if(h[x]==h[y]+1)
      {
        w[++tot]=-y;
        search(y,min(z,d[y][x]));
        if((!t)||(flag))
          return;
        w[tot--]=0;
      }
    }
  }
  if(temp+1!=h[x])
  {
    --g[h[x]];
    if(g[h[x]]==0)
      t=false;
    h[x]=temp+1;
    ++g[h[x]];
  }
}

bool improvable()
{
  flag=false;
  p=n1;
  tot=0;
  w[0]=0;
  search(0,n1);
  return flag;
}

void improve()
{
  for(int x=1;x<=tot;++x)
  {
    if(w[x]>0)
      d[abs(w[x-1])][w[x]]+=p;
    else
      d[-w[x]][abs(w[x-1])]-=p;
  }
}

int main(void)
{
  scanf("%d %d",&m,&lim);
  for(int i=1;i<=m;++i)
  {
    scanf("%s",s+1);
    for(int j=1;j<=m;++j)
    {
      if(s[j]=='Z')
      {
        node he;
        he.r=i;
        he.c=j;
        he.dis=0;
        q.push(he);
      }
      else if(s[j]!='Y')
      {
        ok[i][j]=true;
        d0[i][j]=lim;
        int k=s[j]-'0';
        if(k>0)
        {
          a[i][j]=++n;
          f[0][n]=k;
          n1+=k;
        }
      }
    }
  }
  memcpy(okb,ok,sizeof(ok));
  for(int i=1;i<=m;++i)
  {
    scanf("%s",s+1);
    for(int j=1;j<=m;++j)
    {
      if((s[j]=='Z')||(s[j]=='Y'))
        continue;
      int k=s[j]-'0';
      if(k>0)
      {
        b[i][j]=++n;
        f[n][m*m+1]=k;
      }
    }
  }
  if(n!=m*m)
  {
    for(int i=1;i<=m;++i)
    {
      for(int j=1;j<=m;++j)
      {
        if(b[i][j]>0)
        {
          int k=b[i][j];
          f[k][n+1]=f[k][m*m+1];
          f[k][m*m+1]=0;
        }
      }
    }
  }
  while(!q.empty())
  {
    node he=q.front();
    q.pop();
    int i=he.r,j=he.c;
    d0[i][j]=he.dis;
    for(int k=0;k<4;++k)
    {
      if(ok[i+dx[k]][j+dy[k]])
      {
        node e;
        e.r=i+dx[k];
        e.c=j+dy[k];
        e.dis=he.dis+1;
        q.push(e);
        ok[i+dx[k]][j+dy[k]]=false;
      }
    }
  }
  for(int i=1;i<=m;++i)
  {
    for(int j=1;j<=m;++j)
    {
      if(a[i][j]==0)
        continue;
      memcpy(ok,okb,sizeof(okb));
      ok[i][j]=false;
      node he;
      he.r=i;
      he.c=j;
      he.dis=0;
      q.push(he);
      while(!q.empty())
      {
        node he=q.front();
        q.pop();
        int x=he.r,y=he.c;
        if((b[x][y]>0)&&(he.dis<=d0[x][y]))
          f[a[i][j]][b[x][y]]=n1;
        if((he.dis+1>lim)||(he.dis>=d0[x][y]))
          continue;
        for(int k=0;k<4;++k)
        {
          if(ok[x+dx[k]][y+dy[k]])
          {
            node e;
            e.r=x+dx[k];
            e.c=y+dy[k];
            e.dis=he.dis+1;
            q.push(e);
            ok[x+dx[k]][y+dy[k]]=false;
          }
        }
      }
    }
  }
  g[0]=n+2;
  t=true;
  while((t)&&(h[0]<=n+2))
    while(improvable())
      improve();
  int ans=0;
  for(int i=1;i<=n;++i)
    ans+=d[i][n+1];
  printf("%d\n",ans);
  return 0;
}


#include <bits/stdc++.h>
typedef long long LL;
using namespace std;
#define rep(i,n) for(int i = 0;i < n;i ++)

template<int N>
struct MaxFlow {
	int l[N], tot, cur[N], d[N], u, v, s, t, src, sink;

	struct Edge {
		int v, x, cap;
	} E[111111];

	void add(int u, int v, int cap) {
		E[tot] = (Edge) {v, l[u], cap};
		l[u] = tot ++;
		E[tot] = (Edge) {u, l[v], 0};
		l[v] = tot ++;
	}

	void init() {
		memset(l, -1, sizeof l);
		tot = 0;
	}

	bool bfs() {
		memset(d, -1, sizeof d);
		memcpy(cur, l, sizeof l);
		d[src] = 0;
		queue<int> Q;
		Q.push(src);
		while(!Q.empty()) {
			u = Q.front();
			Q.pop();
			for(int i = l[u]; ~i; i = E[i].x) {
				v = E[i].v;
				if(d[v] == - 1 && E[i].cap) {
					d[v] = d[u] + 1;
					Q.push(v);
				}
			}
		}
		return d[sink] != -1;
	}

	int dfs(int u, int in) {
		if(u == sink)	return in;
		int flow = 0, f;
		for(int &i = cur[u]; ~i; i = E[i].x) {
			v = E[i].v;
			if(d[v] == d[u] + 1 && E[i].cap) {
				if(f=dfs(v, min(in-flow, E[i].cap))) {
					flow += f;
					E[i].cap -= f;
					E[i^1].cap += f;
					if(flow == in)	break;
				}
			}
		}
		if(flow < in)	d[u] = -1;
		return flow;
	}

	const int inf = 1e9;
	int dinic(int _src, int _sink) {
		src = _src, sink = _sink;
		int flow = 0;
		while(bfs())
			flow += dfs(src, inf);
		return flow;
	}
};

int n;

const int N = 15;
char field[N][N], capsule[N][N];
int Z[N][N], S[N][N];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

inline bool In(int i, int j) {
	return i>=0&&i<n&&j>=0&&j<n;
}
MaxFlow<210> F;

int t;
const int inf = 1e9;
inline bool isdigit(char ch) {
	return ch>='1' && ch <='9';
}
void bfs(int x, int y, int d[][N], bool f) {
	int sx = x, sy = y;
	fill(d[0], d[N], inf);
	d[x][y] = 0;
	queue<int> Q;
	Q.push(x);
	Q.push(y);
	if(f && isdigit(capsule[x][y])) {
		F.add(sx*n+sy, sx*n+sy+n*n, inf);
	}
	while(!Q.empty()) {
		x = Q.front();
		Q.pop();
		y = Q.front();
		Q.pop();
		if(d[x][y] == t)	break;
		rep(o, 4) {
			int xx = x + dx[o];
			int yy = y + dy[o];
			if(In(xx, yy) && d[xx][yy] == inf && capsule[xx][yy] != 'Y') {
				if(Z[xx][yy] > d[x][y] + 1 || Z[xx][yy] == d[x][y] + 1 && isdigit(capsule[xx][yy])) {
					d[xx][yy] = d[x][y] + 1;
					if(f && isdigit(capsule[xx][yy])) {
						F.add(sx*n+sy, xx*n+yy+n*n, inf);
					}
					if(Z[xx][yy] > d[x][y] + 1 || !f) {
						Q.push(xx);
						Q.push(yy);
					}
				}
			}
		}
	}
}

int main() {
	int x, y;
	scanf("%d%d", &n, &t);
	int src = n*n*2, sink = src + 1;
	F.init();
	rep(i, n) {
		scanf("%s", field[i]);
		rep(j, n) {
			if(field[i][j] == 'Z') {
				x = i, y = j;
			}
		}
	}
	rep(i, n) {
		scanf("%s", capsule[i]);
		rep(j, n) {
			if(isdigit(capsule[i][j])) {
				F.add(i*n+j+n*n, sink, capsule[i][j]-'0');
			}
		}
	}
	bfs(x, y, Z, false);
	rep(i, n) {
		rep(j, n) {
			if(isdigit(field[i][j])) {
				F.add(src, i*n+j, field[i][j]-'0');
				bfs(i, j, S, true);
			}
		}
	}
	printf("%d\n", F.dinic(src, sink));
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

// Graph stuff from TCR:
struct Edge {       /* Directed edge */
  int from;      /* Index of the source node */
  int to;        /* Index of the destination node */
  double length; /* Length of the edge */
  Edge(int f, int t, double l): from(f), to(t), length(l) {}
};

struct Node {
  vector<int> out; /* Indices of edges that start in this node */
};

struct Graph {         /* Directed graph */
  vector<Node> nodes; /* List of nodes */
  vector<Edge> edges; /* List of edges */

  Graph(int nr_nodes, int nr_edges)
  {
    nodes.resize(nr_nodes);  /* Create nodes, without edges */
    edges.reserve(nr_edges); /* Reserve space for edges */
  }

  void add_edge(int from, int to, double length)
  {
    Edge e = Edge(from, to, length);
    edges.push_back(e); /* Add edge to the list */
    nodes[from].out.push_back(edges.size()-1); /* Add to the source node */
  }

  vector<int> find_augmenting_path(int source, int sink,
      vector<double> flow, double *path_cap)
    /* Used in the max_flow algorithm to find an augmenting path,
     * it returns the edges that are on the augmenting path, or an empty vector
     * if there is no path. The path_cap parameter will be set to the capacity
     * of the path.
     * This implementation uses priority based search to find an augmenting path
     * $O(|edges| \log |nodes|)$
     */
  {
    /* Queue that does highest priority first: */
    set< pair<double, int>, greater< pair<double,int> > > q;
    vector<double> capacity (nodes.size(), 0); /* Capacity to each node */
    vector<int> pred(nodes.size(),-1);         /* Data to find the path */
    vector<int> path;

    capacity[source]=INFINITY;             /* Infinite capacity for source */
    q.insert(make_pair(INFINITY, source)); /* Put source in the queue */

    while ((!q.empty()) && (capacity[sink] == 0)) {
      double cap = q.begin()->first;
      int n = q.begin()->second;
      q.erase(q.begin());

      for (unsigned int i = 0 ; i < nodes[n].out.size(); i++) {
        int e_idx = nodes[n].out[i];
        Edge &e = edges[e_idx];
        double e_cap = e.length - flow[e_idx];

        if ((e_cap > 0) && (min(cap,e_cap) > capacity[e.to])) {
          q.erase(make_pair(capacity[e.to], e.to));
          capacity[e.to] = min(cap,e_cap);
          q.insert(make_pair(capacity[e.to], e.to));
          pred[e.to] = e_idx;
        }
      }
    }

    /* Reconstruct the path */
    *path_cap = capacity[sink];
    {
      int e_idx = pred[sink];
      while (e_idx != -1)
      {
        path.push_back(e_idx);
        e_idx = pred[edges[e_idx].from];
      }
    }

    return path;
  }

  double max_flow(int source, int sink)
    /* Computes the maximum flow through the graph from source to sink using
     * Ford-Fulkerson, combined with a priority first search for the augmenting
     * path.
     * $O(|edges|^2 \log |nodes|)$
     */
  {
    Graph residual(nodes.size(), 2*edges.size());
    vector<double> flow(2*edges.size(), 0);
    vector<int> path;
    double path_cap;
    double max_flow = 0;

    /* Construct residual graph */
    for (unsigned int i=0; i < edges.size(); i++)
    {
      residual.add_edge(edges[i].from, edges[i].to, edges[i].length);
      residual.add_edge(edges[i].to, edges[i].from, 0);
    }

    path=residual.find_augmenting_path(source, sink, flow, &path_cap);
    while (path_cap > 0)
    {
      for (unsigned int i=0; i < path.size(); i++)
      {
        int e_idx = path[i];
        int e_ridx = e_idx + 1;
        if ((e_idx % 2) == 1) e_ridx = e_idx - 1;
        flow[e_idx] = flow[e_idx] + path_cap;
        flow[e_ridx] = flow[e_ridx] - path_cap;
      }
      path=residual.find_augmenting_path(source, sink, flow, &path_cap);
    }

    for (unsigned int i = 0 ; i < residual.nodes[source].out.size(); i++)
    {
      int e_idx = residual.nodes[source].out[i];
      max_flow = max_flow + flow[e_idx];
    }

    // Note: to find a min-cut, divide the graph into all nodes
    // that are reachable from the source via non-saturated edges
    // and those that are not. The endges between those two sets
    // of nodes will have a total capacity of max_flow

    return max_flow;
  }

  void print_all();
};

static int n;
static int t;

struct Room
{
  int i,j;
  array<bool, 61> infected;
  array<bool, 61> accessible;
  int scientists;
  int capsules;

  Room(): scientists{0}, capsules{0}
  {
    fill(infected.begin(), infected.end(), false);
    fill(accessible.begin(), accessible.end(), true);
  }

  int get_node_id(int timestamp) const
  {
    return 2 + i + n*j + (n*n)*timestamp;
  }

  void set_reactor()
  {
    fill(accessible.begin(), accessible.end(), false);
  }

  void set_infected(int timestamp = 0)
  {
    if (accessible[timestamp])
    {
      accessible[timestamp] = false;
      infected[timestamp] = true;
    }
  }

  void spread_infection(int timestamp);

};

static array< array< Room, 10>, 10> floorplan;

void Room::spread_infection(int timestamp)
{
  if (infected[timestamp])
  {
    set_infected(timestamp+1);
    if (i > 0) floorplan[i-1][j].set_infected(timestamp+1);
    if (i < (n-1)) floorplan[i+1][j].set_infected(timestamp+1);
    if (j > 0) floorplan[i][j-1].set_infected(timestamp+1);
    if (j < (n-1)) floorplan[i][j+1].set_infected(timestamp+1);
  }
}

string id_to_string(int id)
{
  if (id == 0) return "source";
  if (id == 1) return "sink";
  stringstream ss;
  int row = (id - 2) % n;
  int col = ((id - 2 - row) % (n*n)) / n;
  int timestamp = (id - 2 - row - col*n) / (n*n);
  ss << "(" << row << "," << col << "," << timestamp << ")";
  return ss.str();
}

void Graph::print_all()
{
  for (int i = 0; i < nodes.size() ; i++)
  {
    Node& node = nodes[i];
    cout << id_to_string(i) << " has edges to : ";
    for (int e = 0; e < node.out.size(); ++e) {
      cout << id_to_string(edges[node.out[e]].to) << " ";
    }
    cout << endl;
  }
}

int main (void)
{
  ios::sync_with_stdio(false);

  cin >> n >> t;

  // Read scientist locations
  for (int i = 0; i < n; i++)
  {
    string line;
    cin >> line;
    for (int j = 0; j < n; j++)
    {
      floorplan[i][j].i = i;
      floorplan[i][j].j = j;
      if (line[j] == 'Y') floorplan[i][j].set_reactor();
      else if (line[j] == 'Z') floorplan[i][j].set_infected();
      else floorplan[i][j].scientists = line[j] - '0';
    }
  }

  // Read capsule locations
  for (int i = 0; i < n; i++)
  {
    string line;
    cin >> line;
    for (int j = 0; j < n; j++)
    {
      if ((line[j] != 'Y') && (line[j] != 'Z')) floorplan[i][j].capsules = line[j] - '0';
    }
  }

  // Figure out the spread of the infection
  for (int c = 0; c <= t; c++)
  {
    for (int i = 0; i < n; i++)
    {
      for (int j = 0; j < n; j++)
      {
        if (c < t) floorplan[i][j].spread_infection(c);
        /*if (floorplan[i][j].infected[c]) cout << "I";
        else if (floorplan[i][j].accessible[c]) cout << ".";
        else cout << "X";*/
      }
      //cout << "\n";
    }
    //cout << "\n\n";
  }

  // Convert to graph
  Graph g(n*n*(t+1)+2, 100*4*60);
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < n; j++)
    {
      const Room &room = floorplan[i][j];
      if (room.scientists > 0) g.add_edge(0, room.get_node_id(0), room.scientists);
      if (room.capsules > 0) g.add_edge(room.get_node_id(t), 1, room.capsules);
      for (int c = 0; c < t; c++)
      {
        if (room.capsules > 0) g.add_edge(room.get_node_id(c), room.get_node_id(t), 1000);
        if (room.accessible[c])
        {
          if (i > 0)
          {
            const Room &other = floorplan[i-1][j];
            if (other.accessible[c]) g.add_edge(room.get_node_id(c), other.get_node_id(c+1), 1000);
          }
          if (i < (n-1))
          {
            const Room &other = floorplan[i+1][j];
            if (other.accessible[c]) g.add_edge(room.get_node_id(c), other.get_node_id(c+1), 1000);
          }
          if (j > 0)
          {
            const Room &other = floorplan[i][j-1];
            if (other.accessible[c]) g.add_edge(room.get_node_id(c), other.get_node_id(c+1), 1000);
          }
          if (j < (n-1))
          {
            const Room &other = floorplan[i][j+1];
            if (other.accessible[c]) g.add_edge(room.get_node_id(c), other.get_node_id(c+1), 1000);
          }
        }
      }
    }
  }

  //g.print_all();

  cout << int(g.max_flow(0,1)) << "\n";

  return 0;
}


#include<map>
#include<cmath>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<string.h>
//#include<windows.h>
#include<algorithm>
#define y1 abcde
#define sqr(x) ((x)*(x))
#define INF 2147483647
using namespace std;

  double ds,x[3],y[3],r[3],R[3];
  int ans;

    double dist(double x,double y,double xx, double yy)
{
      return sqrt((x - xx)*(x - xx) + (y - yy)*(y-yy));
}
int main()
{
   //srand(GetTickCount());
   #ifndef ONLINE_JUDGE
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
   #endif

     cin>>x[1]>>y[1]>>r[1]>>R[1];
     cin>>x[2]>>y[2]>>r[2]>>R[2];

     //
     ds = dist(x[1],y[1],x[2],y[2]);
       if (ds >= R[2]+r[1]) ans++; else
       if (ds + r[1] <= r[2]) ans++; else
       if (ds + R[2] <= r[1]) ans++;

       if (ds >= R[2]+R[1]) ans++; else
       if (ds + R[1] <= r[2]) ans++; else
       if (ds + R[2] <= R[1]) ans++;

       if (ds >= r[2]+R[1]) ans++; else
       if (ds + r[2] <= r[1]) ans++; else
       if (ds + R[1] <= r[2]) ans++;

       if (ds >= R[2]+R[1]) ans++; else
       if (ds + R[2] <= r[1]) ans++; else
       if (ds + R[1] <= R[2]) ans++;
     //
     cout<<ans;
    return 0;
}


#include <bits/stdc++.h>

#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define for1(i, n) for(int i = 1; i <= (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define clr(x) memset((x), 0, sizeof((x)));
#define pb push_back
#define mp make_pair
#define x first
#define y second

using namespace std;
typedef long double ld;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long i64;
typedef unsigned long long ull;

template<class T>
bool remin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool remax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

int nxt() {
    int x;
    scanf("%d", &x);
    return x;
}

ll gcd(ll a, ll b) {
    a = abs(a);
    b = abs(b);
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}

typedef ll ptdata;

struct pt {
    ptdata x, y;

    pt() {}

    pt(ptdata x, ptdata y) : x(x), y(y) {}

    inline pt operator-(const pt &r) const {
        return pt(x - r.x, y - r.y);
    }

    inline pt operator+(const pt &r) const {
        return pt(x + r.x, y + r.y);
    }

    inline pt operator*(const ld &r) const {
        return pt(x * r, y * r);
    }

    inline ptdata sqlen() const {
        return abs(x * x + y * y);
    }

    inline bool operator<(const pt &r) const {
        if (x != r.x) return x < r.x;
        return y < r.y;
    }

    inline bool operator==(const pt &r) const {
        return x == r.x && y == r.y;
    }
};

ostream &operator<<(ostream &os, const pt &p) {
    return os << "(" << p.x << "," << p.y << ")";
}

inline ptdata cross(const pt &l, const pt &r) {
    return l.x * r.y - l.y * r.x;
}

inline ptdata dot(const pt &l, const pt &r) {
    return l.x * r.x + l.y * r.y;
}

ll pwmod(ll a, ll n, ll mod) {
    ll ret = 1;
    while (n) {
        if (n & 1) ret = ret * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return ret;
}

template<typename T>
inline T sqr(T x) {
    return x * x;
}

const int N = 1000 * 1000;
const ll inf = LLONG_MAX >> 1;
struct Q {
    ll q[N];
    ll sum = 0;
    int cnt[N];
    int qs = 0, qsize = 0;
    Q() {}
    inline void add(ll val, int c) {
        sum += c;
        if (empty()) {
            q[qs] = val;
            cnt[qs] = c;
            ++qsize;
            return;
        }
        if (qsize == N) return;
        int ps1 = qs + qsize - 1;
        if (ps1 >= N) ps1 -= N;
        int ps2 = qs + qsize;
        if (ps2 >= N) ps2 -= N;
        if (q[ps1] == val) {
            cnt[ps1] += c;
            return;
        }
        q[ps2] = val;
        cnt[ps2] = c;
        qsize++;
    }
    inline bool empty() const {
        return qsize == 0;
    }
    inline pair<ll, int> min() const {
        if (empty()) return mp(inf, 1);
        return mp(q[qs], cnt[qs]);
    }
    inline pair<ll, int> max() const {
        if (empty()) return mp(-inf, 1);
        int ps1 = qs + qsize - 1;
        if (ps1 >= N) ps1 -= N;
        return mp(q[ps1], cnt[ps1]);
    }
    inline void pop() {
        sum -= cnt[qs];
        ++qs;
        --qsize;
        if (qs == N) qs = 0;
    }
};

Q q[2];

const int MAGIC = 100000;

inline void solve() {
    int n = nxt();
    ll c[2];
    c[0] = nxt();
    c[1] = nxt();
    if (c[0] == 0 || c[1] == 0) {
        ll m = c[0] + c[1];
        cout << (n - 1) * m << "\n";
        return;
    }
    q[0].add(0, 1);

    ll ans = 0;
    pair<ll, int> mi[2];
    pair<ll, int> ma[2];
    --n;
    while (n > 0) {
        forn(j, 2) {
            mi[j] = q[j].min();
            ma[j] = q[j].max();
        }
        int id = 0;
        if (mi[1].first < mi[0].first) id = 1;

        ll k = n;
        if (c[id]) {
            k = (mi[id ^ 1].first - ma[id].first) / c[id];
        }
        remin(k, (n / q[id].sum));

        if (k == 0 || q[id ^ 1].qsize < MAGIC) {
            int cc = min(mi[id].second, n);
            n -= cc;
            ans -= mi[id].first * cc;
            ans += mi[id].first * cc + c[0] * cc;
            ans += mi[id].first * cc + c[1] * cc;
            mi[id].second -= cc;
            if (mi[id].second == 0) {
                q[id].pop();
            }
            q[0].add(mi[id].first + c[0], cc);
            q[1].add(mi[id].first + c[1], cc);
        } else {
//            cerr << k << "\n";
//            cerr << q[id].qsize << "\n";
            vector<pair<ll, int> > w;
            while (!q[id].empty()) {
                w.push_back(q[id].min());
                q[id].pop();
            }
            for (auto pa : w) {
                ans -= pa.first * pa.second;
            }
            for (auto pa : w) {
//                cerr << pa.first * pa.second + k * c[id] * pa.second << "\n";
//                cerr << k * pa.first * pa.second + k * (k - 1) / 2 * c[id] + k * c[id ^ 1] * pa.second << "\n";
                ans += pa.first * pa.second + k * c[id] * pa.second;
                ans += k * pa.first * pa.second + k * (k - 1) / 2 * c[id] + k * c[id ^ 1] * pa.second;
                q[id].add(pa.first + k * c[id], pa.second);
            }
            n -= k * q[id].sum;
        }
    }
    cout << ans << "\n";
}

void prepare() {

}

int main() {
#ifdef LOCAL
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    prepare();

    int t = 1;
    //t = nxt();
    forn(i, t) {
        solve();
    }
    //14999999850000000
    //195405044762803700
    //9999999999999999
    //100000000 1 100000000
    //20042860182236000
    cerr << "Time " << clock() / (double) CLOCKS_PER_SEC << endl;
    return 0;
}


//加油吧
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353,N=200200,G=3,Gi=332748118;
ll power(ll base,ll b){
	ll ret=1;
	while(b){
		if(b&1)ret=ret*base%mod;
		base=base*base%mod;
		b>>=1;
	}
	return ret;
}
int limit,len,last=-1,pos[N];
ll G0[N],G1[N],G2[N],g[N],F0[N],F1[N],F2[N];
//F0是两端都没有距离为2的点
//F1是一端有距离为2的点  (答案统计时有两种方案数相同的情况)
//F2是两端都有距离为2的点
inline void getlen(int n){
	limit=1,len=0;
	while(limit<n)limit<<=1,++len;
	if(last==limit)return;
	last=limit;
	for(int i=1;i<limit;++i)
		pos[i]=(pos[i>>1]>>1)|((i&1)<<(len-1));
}
inline void ntt(ll *a,int n,int type){
	for(int i=0;i<limit;++i)
		if(i<pos[i])swap(a[i],a[pos[i]]);
	for(int mid=1;mid<limit;mid<<=1){
		ll wn=power((type==1?G:Gi),(mod-1)/(mid<<1));
		for(int j=0;j<limit;j+=(mid<<1)){
			ll w=1;
			for(int k=0;k<mid;++k,w=w*wn%mod){
				ll x=a[j+k],y=a[j+mid+k]*w%mod;
				a[j+k]=(x+y)%mod;
				a[j+mid+k]=(x-y+mod)%mod;
			}
		}
	}
	if(type==-1){
		ll inv=power(limit,mod-2);
		for(int i=0;i<n;++i)a[i]=a[i]*inv%mod;
		for(int i=n;i<limit;++i)a[i]=0;
	}
}
ll f[N],h[N],tmp[N];
void calc(ll *a,int n,ll *b,int m,ll *c,int nm=-1){
	if(nm==-1)nm=n+m-1;
	getlen(nm);
	for(int i=0;i<n;++i)f[i]=a[i];
	for(int i=n;i<limit;++i)f[i]=0;
	for(int i=0;i<m;++i)h[i]=b[i];
	for(int i=m;i<limit;++i)h[i]=0;
	ntt(f,nm,1),ntt(h,nm,1);
	for(int i=0;i<limit;++i)f[i]=f[i]*h[i]%mod;
	ntt(f,nm,-1);
	for(int i=0;i<nm;++i)c[i]=f[i];
}
void CDQ(int l,int r){
	if(l==r){
		F0[l]=(F0[l]+G0[l])%mod;
		F1[l]=(F1[l]+G1[l])%mod;
		F2[l]=(F2[l]+G2[l])%mod;
		return;
	}
	int mid=(l+r)>>1;
	CDQ(l,mid);
	calc(F0+l,mid-l+1,G0,r-l+1,tmp+l,r-l+1);
	for(int i=mid+1;i<=r;++i)F0[i]=(F0[i]+tmp[i])%mod;
	calc(F0+l,mid-l+1,G1,r-l+1,tmp+l,r-l+1);
	for(int i=mid+1;i<=r;++i)F1[i]=(F1[i]+tmp[i])%mod;
	calc(F1+l,mid-l+1,G1,r-l+1,tmp+l,r-l+1);
	for(int i=mid+1;i<=r;++i)F0[i]=(F0[i]+tmp[i])%mod;
	for(int i=mid+1;i<=r;++i)F2[i]=(F2[i]+tmp[i])%mod;
	calc(F1+l,mid-l+1,G2,r-l+1,tmp+l,r-l+1);
	for(int i=mid+1;i<=r;++i)F1[i]=(F1[i]+tmp[i])%mod;
	calc(F2+l,mid-l+1,G2,r-l+1,tmp+l,r-l+1);
	for(int i=mid+1;i<=r;++i)F2[i]=(F2[i]+tmp[i])%mod;
	//注意是左边对右边的贡献
	CDQ(mid+1,r);
}
int n;
int main(){
//	freopen("y.in","r",stdin);
	scanf("%d",&n);
	g[0]=g[2]=1;
	for(int i=4;i<=n;++i)g[i]=(g[i-2]+g[i-4])%mod;
	for(int i=1;i<=n;++i)G0[i]=1LL*(i-1)*(i-1)%mod*g[i-1]%mod;
	for(int i=2;i<=n;++i)G1[i]=1LL*(i-1)*(i-1)%mod*g[i-2]%mod;
	for(int i=3;i<=n;++i)G2[i]=1LL*(i-1)*(i-1)%mod*g[i-3]%mod;
	CDQ(1,n);
	ll Ans=1LL*n*(G0[n]+G2[n])%mod;//可以旋转n次
	for(int i=2;i<=n-2;++i){
		Ans=(Ans+(G0[i]*F0[n-i]%mod+2LL*G1[i]*F1[n-i]%mod+G2[i]*F2[n-i]%mod)*(ll)i%mod)%mod;//把第一个相对的点对旋转到1-n+1处
	}
	printf("%lld\n",(Ans%mod+mod)%mod);
	return 0;
}


//khodaya khodet komak kon
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimize ("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-O2")

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 1000000 + 10;
const ll MOD = 1000000000 + 7;
const ll INF = 1000000010;
const ll LOG = 25;
const ll K = 100000000;

map<vector<int>, int> mp;
vector<vector<int>> V;
int dis[1300][1300], q, pnt, mn[1300][1300], mark[N];
ll num[N];
int P[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
vector<int> vv, D[N], G[N];

void BT(int x = 0, ll n = 1, int mx = 41){
	if (x == 10){
		vector<int> KK = vv;
		reverse(all(KK));
		mp[KK] = ++pnt;
		V.pb(KK);
		return;
	}
	for (int i = 0; i <= mx && n < K; i++){
		vv.pb(i);
		BT(x + 1, n, i);
		vv.pop_back();
		n *= P[x];
	}
}

void BFS(int v){
	dis[v][v] = 0;
	//cout << v << endl;
	queue<int> Q;
	Q.push(v);

	while (Q.size()){
		//cout << "YES2" << endl;
		int fr = Q.front();
		mn[v][num[fr]] = min(mn[v][num[fr]], dis[v][fr]);
		Q.pop();
		for (auto u:G[fr]){
			if (dis[v][u] > dis[v][fr] + 1){
				dis[v][u] = dis[v][fr] + 1;
				Q.push(u);
			}
		}
	}
}

int32_t main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> q;
	BT();
//	cout << pnt<< endl;
	mark[1] = 1;
	for (int i = 1; i < N; i++){
		if (!mark[i]){
			for (int j = i; j < N; j += i){
				mark[j] = 1;
				int x = j;
				int cnt = 0;
				while (x % i == 0){
					x /= i;
					cnt++;
				}
				D[j].pb(cnt);
			}
		}
		while (D[i].size() < 10) D[i].pb(0);
		sort(all(D[i]));
	}
	vector<int> adad;
	for (vector<int> u:V){
		for (int i = 0; i < 10; i++){
			vector<int> uu;
			for (auto x:u) uu.pb(x);
			uu[i] --;
			sort(all(uu));
			if (mp.count(uu)) G[mp[uu]].pb(mp[u]), G[mp[u]].pb(mp[uu]);
			uu.clear();
			for (auto x:u) uu.pb(x);
			uu[i] ++;
			sort(all(uu));
			if (mp.count(uu)) G[mp[uu]].pb(mp[u]), G[mp[u]].pb(mp[uu]);;
		}
		ll res = 1;
		for (auto x:u) res *= x + 1;
		num[mp[u]] = res;
		adad.pb(res);
	}
	sort(all(adad));
	adad.resize(unique(all(adad)) - adad.begin());
	for (int i = 1; i <= pnt; i++){
		num[i] = lower_bound(all(adad), num[i]) - adad.begin() + 1;
	}
	//cout << pnt << endl;
//	cout << "YES" << endl;
	memset(mn, 31, sizeof mn);
	memset(dis, 31, sizeof dis);
	for (int i = 1; i <= pnt; i++) BFS(i);
	//cout << dis[mp[D[9]]][mp[D[3]]] << '\n';
	for (int i = 1; i <= q; i++){
		int a, b;
		cin >> a >> b;
		if (!mp.count(D[a]) || !mp.count(D[b])) assert(0);
		a = mp[D[a]];
		b = mp[D[b]];
		int ans = 10000;
		for (int j = 1; j <= pnt; j++){
			ans = min(ans, mn[a][j] + mn[b][j]);
		}
		cout << ans << '\n';
	}

	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define mp make_pair
#define pu push
#define po pop
#define N 100005
#define FAST ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);srand(time(NULL));
using namespace std;
ll n1,n2,x,y,i=0,j;
string a,b,ans;
bool flag=0;
int main(void){
	cin>>a>>b;
	sort(a.begin(),a.end());

	n1=a.length();
	n2=b.length();

	if(n2>n1)return reverse(a.begin(),a.end()),cout<<a,0;

	for(i=0;i<n1;i++){
		for(j=i+1;j<n1;j++){
			string t=a;
			swap(t[i],t[j]);
			if(stoll(t)>=stoll(a) && stoll(t)<=stoll(b))
			a=t;
		}
	}
	cout<<a;
	return 0;
}


// tarptaeya
#include <bits/stdc++.h>
// base types
#define _i int
#define _l long
#define _ll long long
#define _f float
#define _d double
#define _ld long double
// containters
#define _str std::string
#define _v std::vector
#define _vi std::vector<int>
#define _vl std::vector<long>
#define _vll std::vector<long long>
#define _vstr std::vector<std::string>
#define _vvi std::vector<std::vector<int> >
#define _psb push_back
#define _ppb pop_back
#define _s std::set
#define _si std::set<int>
#define _sl std::set<long>
#define _sll std::set<long long>
#define _sstr std::set<std::string>
#define _ins insert
#define _rem remove
#define _p std::pair
#define _m std::map
#define _q std::queue
#define _ps push
#define _pp pop
// algorithms
#define _sort(a) std::sort(a.begin(), a.end())
// io
#define _fio std::ios_base::sync_with_stdio(0); std::cin.tie(0)
#define _in std::cin
#define _ina(a, n) for (decltype(n) i = 0; i <  (n); i++) { std::cin >> a[i]; }
#define _out std::cout
#define _outa(a) for (auto i : a) std::cout << i << ' '; std::cout << std::endl;
#define _nl std::endl
// general
#define _rep(i, a, b) for (decltype(b) (i) = (a); i < (b); i++)
#define _till while
#define _max(a, b) (a) > (b) ? (a) : (b)
#define _min(a, b) (a) < (b) ? (a) : (b)
#define _for(i,a) for (auto i : a)
#define _ord(a) a - '0'
#define _mod 1000000007
// https://stackoverflow.com/a/47434423
#define _inf (unsigned)!((int)0)
#define _pi 3.141592653589
#define _eps 0.000000001

int main()
{
    _d n, R, r;
    _in >> n >> R >> r;
    if (R == r || r / (R - r) > 1) {
        if (n == 1 && (R - r < _eps || R > r)) {
            _out << "YES" << _nl;
        } else {
            _out << "NO" << _nl;
        }
    } else {
        _d t = asin(r / (R - r));
        //_out << _pi / t - n << _nl;
        if (_pi / t > n || (_pi / t > n ? _pi / t - n : n - _pi / t) < _eps) {
            _out << "YES" << _nl;
        } else {
            _out << "NO" << _nl;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;

int pot2[20];

int main() {
	pot2[0]=1;
	for (int i=1;i<20;i++){
		pot2[i]=2*pot2[i-1];
	}
	int n,l,r,max_sum=0,min_sum=0;
	cin>>n>>l>>r;
	for(int i=0;i<r;i++){
		if(i<l) min_sum+=pot2[i];
		max_sum+=pot2[i];
	}
	min_sum+=n-l;
	max_sum+=pot2[r-1]*(n-r);
	cout<<min_sum<<" "<<max_sum;
	return 0;
}


#include<bits/stdc++.h>
#define pa pair<int,int>
typedef unsigned long long ull;
typedef long long ll;
using namespace std;
const int N=1e7+10;
int inf=1e9;
const int mod =998244353;
inline int qpow(int a,int b,int Mod){
	int ans=1;
	while(b){
		if(b&1) ans=(1ll*ans*a%Mod);
		b>>=1;a=1ll*a*a%Mod;
	}
	return ans;
}

inline int rd(){
	int x=0;int f=1;char s=getchar();
	while(!isdigit(s)){ if(s=='-')f=-1;s=getchar();}
	while(isdigit(s)){ x=(x<<1)+(x<<3)+s-'0';s=getchar(); }
	return f==1?x:-x;
}
int cas;
ll n,x,y,k,M;
ll dp[720721][18];
ll a[N];
ll fac[20];
int main(){

	cin>>n>>a[1]>>x>>y>>k>>M;
	for(int i=2;i<=n;i++) a[i]=(x*a[i-1]+y)%M;
//	for(int i=1;i<=n;i++) cout<<a[i]<<" ";cout<<endl;

	fac[0]=1;int lcm=1;
	for(int i=1;i<k;i++){
		fac[i]=fac[i-1]*n%mod;
		lcm=1ll*lcm*i/__gcd(i,lcm);
	}
//	cout<<lcm<<endl;
	ll ans=0;
	memset(dp,0,sizeof dp);
	for(int i=1;i<=n;i++) {
		ans=(ans+lcm*(a[i]/lcm)%mod*k%mod*fac[k-1]%mod)%mod;
		a[i]%=lcm;
		dp[a[i]][0]++;
	}
//	cout<<ans<<endl;
	for(int j=0;j<k;j++){
		for(int i=0;i<lcm;i++){
			dp[i][j+1]=(dp[i][j+1] + (n-1)*dp[i][j]%mod)%mod;
			dp[i-(i%(j+1))][j+1]=(dp[i-(i%(j+1))][j+1] + dp[i][j])%mod;
		}
	}

	for(int j=0;j<k;j++){
		for(int i=0;i<lcm;i++){
			ans=(ans+i*dp[i][j]%mod*fac[k-j-1]%mod)%mod;
		}
	}
	cout<<ans<<endl;

	return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int k;
    while(cin>>k)
    {
        k+=k;
        int a[10]={};
        int flag=1;
        for(int i=0;i<16;i++)
        {
            char ch;
            cin>>ch;
            if(ch>='0'&&ch<='9')
            {
                a[ch-'0']++;
                if(a[ch-'0']>k) flag=0;
            }
        }
        if(flag==1) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int N=100005;
int INF=0x3f3f3f3f;
LL mod=1e9+7;
int main(){
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    string s[13]={
        "",
        "x^y^z",
        "x^z^y",
        "(x^y)^z",
        "(x^z)^y",
        "y^x^z",
        "y^z^x",
        "(y^x)^z",
        "(y^z)^x",
        "z^x^y",
        "z^y^x",
        "(z^x)^y",
        "(z^y)^x",
    };
    int ans=1;
    long double x,y,z;
    long double mx=-10000,t;
    cin>>x>>y>>z;
    if(x<=1&&y<=1&&z<=1){
        ans=1;
        mx=pow(x,pow(y,z));
        t=pow(x,pow(z,y));
        if(t>mx){
            mx=t;
            ans=2;
        }
        t=pow(pow(x,y),z);
        if(t>mx){
            mx=t;
            ans=3;
        }
        t=pow(y,pow(x,z));
        if(t>mx){
            mx=t;
            ans=5;
        }
       t=pow(y,pow(z,x));
        if(t>mx){
            mx=t;
            ans=6;
        }
        t=pow(pow(y,x),z);
        if(t>mx){
            mx=t;
            ans=7;
        }
        t=pow(z,pow(x,y));
        if(t>mx){
            mx=t;
            ans=9;
        }
        t=pow(z,pow(y,x));
        if(t>mx){
            mx=t;
            ans=10;
        }
        t=pow(pow(z,x),y);
        if(t>mx){
            mx=t;
            ans=11;
        }
    }
    else{
        //cout<<"go"<<endl;
        if(x>1){
            t=z*log(y)+log(log(x));
            if(t>mx){
                mx=t;
                ans=1;
            }
            t=y*log(z)+log(log(x));
            if(t>mx){
                mx=t;
                ans=2;
            }
            t=log(y)+log(z)+log(log(x));
            if(t>mx){
                mx=t;
                ans=3;
            }
        }
        if(y>1){
            t=z*log(x)+log(log(y));
            if(t>mx){
                mx=t;
                ans=5;
            }
            t=x*log(z)+log(log(y));
            if(t>mx){
                mx=t;
                ans=6;
            }
            t=log(x)+log(z)+log(log(y));
            if(t>mx){
                mx=t;
                ans=7;
            }
        }
        if(z>1){
            t=y*log(x)+log(log(z));
            if(t>mx){
                mx=t;
                ans=9;
            }
            t=x*log(y)+log(log(z));
            if(t>mx){
                mx=t;
                ans=10;
            }
            t=log(x)+log(y)+log(log(z));
            if(t>mx){
                mx=t;
                ans=11;
            }
        }
    }

    cout<<s[ans]<<endl;
    return 0;
}


#include <algorithm>
#include <iostream>
#include <string>
#include <stack>

int main()
{
	int n, x, y, t, a = 0, b = 0;
	std::cin >> n;
	t = std::sqrt(double(n));
	for (y = 1; y <= t; ++y)
	{
		x = n / y;
		if (x*y == n)
		{
			a = y;
			b = x;
		}
	}
	std::cout << a << ' ' << b;
	return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;

#define     LL                      long long int
#define     LD                      long double
#define     ULL                     unsigned long long int

#define     pii                     pair<int, int>
#define     ump                     unordered_map
#define     mp                      make_pair
#define     mt                      make_tuple
#define     pb                      push_back
#define     ff                      first
#define     ss                      second
#define     SZ(x)                   (int)x.size()
#define     all(x)                  x.begin(), x.end()
#define     lb(x, y)                lower_bound(all(x), y)
#define     fill2d(arr, a, b, v)    fill(&arr[0][0], &arr[0][0] + a * b, v)
#define     fill3d(arr, a, b, c, v) fill(&arr[0][0], &arr[0][0] + a * b * c, v)
#define     rep(i, begin, end)      for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

#define     sp(x)                   setprecision(x) << fixed
#define     what_is(x)              cout << #x << " is " << x << endl
//#define     I64                     ll

#define     PI                      acos(-1.0)
#define     EPS                     1e-12
#define     mx                      200010
#define     S_SIZE                  2000010

const LL inf = 1000000000;
const LL mod = 1000000000 + 7;

inline void IO() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); }
inline int dcmp (LD x) { return x < -EPS ? -1 : (x > EPS); }

template<class T> inline int CHECK(T MASK, int i) { return (MASK >> i) & 1; }
template<class T> inline T ON(T MASK, int i) { return MASK | (T(1) << i); }
template<class T> inline T OFF(T MASK, int i) { return MASK & (~(T(1) << i)); }
template<typename T> inline int CNT(T MASK) {
    if (numeric_limits<T>::digits <= numeric_limits<unsigned int>::digits) return __builtin_popcount(MASK);
    else return __builtin_popcountll(MASK);
}
template<class T> inline int RIGHT(T MASK) { return log2(MASK & -MASK); }

int dx4[] = { 0, 0, -1, +1 }; int dy4[] = { +1, -1, 0, 0 };
int dx8[] = { 1, 1, 0, -1, -1, -1, 0, 1, 0 }; int dy8[] = { 0, 1, 1, 1, 0, -1, -1, -1, 0 };

inline void I(int& a) { scanf("%d", &a); } inline void I(LL& a) { scanf("%I64d", &a); }
inline void I(ULL& a) { scanf("%I64u", &a); } inline void I(char* a) { scanf("%s", a); }
char Iarr[S_SIZE]; inline void I(string& a) { scanf("%s", Iarr); a = Iarr; }
template<typename T, typename... Args>
void I(T& a, Args&... args) { I(a); I(args...); }

inline void OUT(int a) { printf("%d", a); } inline void OUT(LL a) { printf("%I64d", a); }
inline void OUT(const char* a) { printf("%s", a); } inline void OUT(char* a) { printf("%s", a); }
inline void OUT(bool a) { printf("%d", a); } inline void OUT(string a) { rep(it, a.begin(), a.end()) printf("%c", *it); }
inline void OUT(ULL a) { printf("%I64u", a); }
template<typename T, typename... Args>
void OUT(T a, Args... args) { OUT(a); OUT(" "); OUT(args...); }
template<typename... Args>
void O(Args... args) { OUT(args...); OUT("\n"); }

template<typename T1, typename T2>
inline ostream& operator<<(ostream& os, pair<T1, T2> p) { os << "{" << p.first << ", " << p.second << "}"; return os; }
template<typename T>
inline ostream& operator<<(ostream& os, vector<T>& a) { os << "["; for (int i = 0; i < (int)a.size(); i++) { if (i) os << ", "; os << a[i]; } os << "]"; return os; }

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cout << *it << " = " << a << endl;
	err(++it, args...);
}

// mp.reserve(1024); mp.max_load_factor(0.25);
// head

const int M = 1000010; // maximum number of cubes

struct NumberTheory {
    LL gcd(LL a, LL b) { while (b) { a %= b; swap(a, b); } return a; }
    LL lcm(LL a, LL b) { return (a / gcd(a, b)) * b; }
    LL bigMod(LL p, LL e, LL m) {
        LL ret = 1; p %= m;
        while (e > 0) { if (e & 1) ret = (ret * p) % m; p = (p * p) % m; e >>= 1; }
        return ret;
    }
    /// (1/a) % m when a & m are co-primes
    LL modInverse(LL a, LL m) {
        LL g = gcd(a, m);
        if (g != 1) {
            cout << "Inverse doesn't exist" << endl; return -1;
        }
        LL m0 = m;
        LL y = 0, x = 1;
        if (m == 1) return 0;
        while (a > 1) {
            LL q = a / m;
            LL t = m;
            m = a % m, a = t;
            t = y;
            y = x - q * y;
            x = t;
        }
        if (x < 0) x += m0;
        return x;
    }
    /// generate all (1/i) % m in [1..n] where n is less than m
    /// m must be prime
    void genModInv(int n, LL m, ULL inv[]) {
        inv[1] = 1;
        for (int i = 2; i <= n; ++i)
            inv[i] = (m - (m / i) * inv[m%i] % m) % m;
    }
    /// generate all (1/i!) % m in [1..n] where n is less than m
    /// m must be prime
    void genModInvFact(int n, LL m, ULL inv[], ULL invFact[]) {
        invFact[0] = invFact[1] = 1;
        for (int i = 2; i <= n; i++)
            invFact[i] = (invFact[i-1] * inv[i]) % m;
    }
} num;

struct CRT
{
    long long GCD(long long a, long long b) { return (b == 0) ? a : GCD(b, a % b); }
    inline long long LCM(long long a, long long b) { return a / GCD(a, b) * b; }
    inline long long normalize(long long x, long long mod) { x %= mod; if (x < 0) x += mod; return x; }
    struct GCD_type { long long x, y, d; };
    GCD_type ex_GCD(long long a, long long b)
    {
        if (b == 0) return {1, 0, a};
        GCD_type pom = ex_GCD(b, a % b);
        return {pom.y, pom.x - a / b * pom.y, pom.d};
    }
    // Helper Functions

    pair<long long, long long> calc(int t, long long aa[], long long nn[])
    {
        // Make sure the arrays are 1 indexed
        long long* a = new long long[t+1];
        long long* n = new long long[t+1];
        long long ans, lcm;
        for(int i = 1; i <= t; i++) {
            a[i] = aa[i];
            n[i] = nn[i];
            normalize(a[i], n[i]);
        }
        ans = a[1];
        lcm = n[1];
        for(int i = 2; i <= t; i++) {
            auto pom = ex_GCD(lcm, n[i]);
            int x1 = pom.x;
            int d = pom.d;
            if((a[i] - ans) % d != 0) return cerr << "No solutions" << endl, mp(-1, -1);
            ans = normalize(ans + x1 * (a[i] - ans) / d % (n[i] / d) * lcm, lcm * n[i] / d);
            lcm = LCM(lcm, n[i]); // you can save time by replacing above lcm * n[i] /d by lcm = lcm * n[i] / d
        }
        return make_pair(ans, lcm);
    }
    // t is the number of congruences
    // Array is 1 indexed
    // x mod n1 = a1 mod n1
    // x mod n2 = a2 mod n2
    // x mod n3 = a3 mod n3
    // Here there is an unique value for a certain range.
    // This range will be n1 * n2 * n3 if all of them
    // are pairwise co-prime. But can be different otherwise.
    // The unique value is stored in "ans" and the range is
    // stored in "lcm".
    // The value repeats after "lcm".
    // If all the n's are not pairwise co-prime then there
    // might not be an answer.
} crt;

int main()
{
    LL a, b, p, x;
    I(a, b, p, x);
    LL res = 0;
    for (int i = 1; i <= p - 1; i++) {
        int t = 2;
        long long a1[3], n[3];
        n[1] = p - 1;
        n[2] = p;
        a1[1] = i;
        a1[2] = (b * num.modInverse(num.bigMod(a, i, p), p)) % p;
        pair<LL, LL> temp = crt.calc(t, a1, n);
        if (temp.ff <= x)
            res += (x - temp.ff) / temp.ss + 1;
    }
    O(res);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define fir first
#define sec second
#define ll long long
#define int long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read()
{
	char ch=getchar(); int nega=1; while(!isdigit(ch)) {if(ch=='-') nega=-1; ch=getchar();}
	int ans=0; while(isdigit(ch)) {ans=ans*10+ch-48;ch=getchar();}
	if(nega==-1) return -ans;
	return ans;
}
vector<int> rev,rt,one(1,1),zer;

#undef int

namespace Poly
{
	#define mod 31525197391593473LL
	#define int long long
	#define ull unsigned long long
	inline int add(int x,int y){return x+y>=mod?x+y-mod:x+y;}
	inline int add(int x,int y,int z){return add(add(x,y),z);}
	inline int sub(int x,int y){return x-y<0?x-y+mod:x-y;}
	// inline ull mul(ull x,ull y){return (x*y-(ull)((long double)x/mod*y)*mod+mod)%mod;}
	inline int mul(int x,int y){return (__int128)x*y%mod;}
	inline int mul(int x,int y,int z){return mul(mul(x,y),z);}
	int qpow(int x,int y)
	{
		int ans=1;
		while(y)
		{
			if(y&1) ans=mul(ans,x);
			x=mul(x,x);
			y>>=1;
		}
		return ans;
	}
	int getInv(int x)
	{
		return qpow(x,mod-2);
	}
	int __inv[2000005];
	const int G=3;
	void Init_Inv()
	{
		__inv[0]=__inv[1]=1;
		for(int i=2;i<=2000000;i++) __inv[i]=mul(mod-mod/i,__inv[mod%i]);
	}
	void init(int B)
	{
		int n=1<<B;
		rev.resize(n),rt.resize(n);
		for(int i=1;i<n;i++) rev[i]=(rev[i>>1]>>1)|((i&1)<<(B-1));
		for(int mid=1;mid<n;mid<<=1)
		{
			int w=qpow(G,(mod-1)/(mid<<1));
			rt[mid]=1;
			for(int i=1;i<mid;i++) rt[mid+i]=mul(rt[mid+i-1],w);
		}
	}
	void NTT(vector<int> &a,int B)
	{
		int n=1<<B;
		for(int i=0;i<n;i++) if(rev[i]>i) swap(a[i],a[rev[i]]);
		for(int mid=1;mid<n;mid<<=1)
		{
			for(int i=0;i<n;i+=(mid<<1))
			{
				for(int j=0;j<mid;j++)
				{
					int x=a[i+j],y=mul(a[i+j+mid],rt[j+mid]);
					a[i+j]=add(x,y),a[i+j+mid]=sub(x,y);
				}
			}
		}
	}
	vector<int> Mul(vector<int> a,vector<int> b)
	{
		int B=0,n=a.size()+b.size()-1;
		while((1<<B)<n) B++;
		a.resize(1<<B),b.resize(1<<B);
		init(B);
		NTT(a,B),NTT(b,B);
		for(int i=0;i<(1<<B);i++) a[i]=mul(a[i],b[i]);
		reverse(a.begin()+1,a.end());
		NTT(a,B);
		a.resize(n);
		int In=getInv(1<<B);
		for(int i=0;i<n;i++) a[i]=mul(a[i],In);
		return a;
	}
	#undef mod
};
#undef int

#define int long long
int n,m,mod;
inline int add(int x,int y){return x+y>=mod?x+y-mod:x+y;}
inline int add(int x,int y,int z){return add(add(x,y),z);}
inline int sub(int x,int y){return x-y<0?x-y+mod:x-y;}
inline int mul(int x,int y){return 1LL*x*y%mod;}
inline int mul(int x,int y,int z){return mul(mul(x,y),z);}
vector<int> _QAQ;
vector<int> operator * (vector<int> x,vector<int> y)
{
	vector<ll> a(x.size()),b(y.size());
	for(int i=0;i<(int)x.size();i++) a[i]=x[i];
	for(int i=0;i<(int)y.size();i++) b[i]=y[i];
	vector<ll> r=Poly::Mul(a,b);
	vector<int> ans(r.size());
	for(int i=0;i<(int)r.size();i++) ans[i]=r[i]%mod;
	if((int)ans.size()>n+1) ans.resize(n+1);
	return ans;
}
vector<int> operator + (vector<int> a,vector<int> b)
{
	int n=max(a.size(),b.size());
	a.resize(n),b.resize(n);
	for(int i=0;i<n;i++) a[i]=add(a[i],b[i]);
	return a;
}
vector<int> operator - (vector<int> a,vector<int> b)
{
	int n=max(a.size(),b.size());
	a.resize(n),b.resize(n);
	for(int i=0;i<n;i++) a[i]=sub(a[i],b[i]);
	return a;
}
vector<int> a[200],b[200];
int cnt;
map<int,int> vis;
void print(vector<int> v)
{
	cout<<"-- ";
	for(int i :v) printf("%d ",i);
	cout<<"\n";
}
int solve(int x)
{
	if(vis[x]) return vis[x];
	vis[x]=++cnt;
	int cur=cnt;
	if(x==0)
	{
		a[cur]=b[cur]=zer;
		return cur;
	}
	int V=solve(x/2);
	vector<int> A=a[V],B=b[V];
	// print(A),print(B);
	if(x%2==0)
	{
		if((x/2)&1) a[cur]=A+(A+B+one)*B,b[cur]=B+(A+B+one)*A;
		else a[cur]=A+(A+B+one)*A,b[cur]=B+(A+B+one)*B;
	}
	else
	{
		int R=solve(x-1);
		vector<int> A=a[R],B=b[R];
		if(x&1) B=B+(A+B+one)*_QAQ;
		else A=A+(A+B+one)*_QAQ;
		a[cur]=A,b[cur]=B;
	/*
		vector<int> C=A,D=B;
		if((x/2)&1) C=C+(A+B+one)*_QAQ;
		else D=D+(A+B+one)*_QAQ;
		if((x/2)&1) a[cur]=A+(A+B+one)*C,b[cur]=A+(A+B+one)*D;
		else a[cur]=A+(A+B+one)*D,b[cur]=A+(A+B+one)*C;
	*/
	}
	return cur;
}
signed main()
{
	Poly::Init_Inv();
	_QAQ.resize(2); _QAQ[1]=1; zer.resize(1);
	// cout<<_QAQ[1]<<endl;
	cin>>n>>m>>mod;
	int ans=0;
	for(ll i=2;i<=m;i*=2)
	{
		int pl=solve(m/i);
		// printf("%d :\n",m/i);
		// for(int i=0;i<(int)a[pl].size();i++) printf("%d ",a[pl][i]); cout<<"\n";
		// for(int i=0;i<(int)b[pl].size();i++) printf("%d ",b[pl][i]); cout<<"\n";
		for(int i=1;i<(int)b[pl].size();i+=2) ans=add(ans,b[pl][i]);
	}
	cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAX_N=100005;
int n,m;
int H(int x)
{
	if(x>=0)return 1;
	else return 0;
}
bool legal(int nx,int ny,int dx,int dy)
{
	if(nx==1 && ny==1 && dx==-1 && dy==-1)return false;
	if(nx==1 && ny==m && dx==-1 && dy==1)return false;
	if(nx==n && ny==1 && dx==1 && dy==-1)return false;
	if(nx==n && ny==m && dx==1 && dy==1)return false;
	return true;
}
map<int,int> vis[2][2][MAX_N];
map<int,int> used[MAX_N];
signed main()
{
	int nx,ny,dx,dy;
	cin>>n>>m;
	cin>>nx>>ny;
	string s;
	cin>>s;
	if(s[0]=='U')dx=-1;
	else dx=1;
	if(s[1]=='L')dy=-1;
	else dy=1;
	int ans=1;
	int cnt=0;
	while(true)
	{
	//	cout<<nx<<' '<<ny<<' '<<dx<<' '<<dy<<endl;
		if(!used[nx][ny])cnt++;
		used[nx][ny]=true;
		if(cnt==n+m-2)
		{
			cout<<ans<<endl;
			return 0;
		}
		if(nx==1)dx=1;
		if(ny==1)dy=1;
		if(nx==n)dx=-1;
		if(ny==m)dy=-1;
		if(vis[H(dx)][H(dy)][nx][ny])
		{
			cout<<-1<<endl;
			return 0;
		}
		vis[H(dx)][H(dy)][nx][ny]=true;
	/*	if(!legal(nx,ny,dx,dy))
		{
			cout<<-1<<endl;
			return 0;
		}*/
		if(dx==1 && dy==1)
		{
			int d1=n-nx;
			int d2=m-ny;
			int d=min(d1,d2);
			nx+=d;
			ny+=d;
			ans+=d;
		}
		if(dx==1 && dy==-1)
		{
			int d1=n-nx;
			int d2=ny-1;
			int d=min(d1,d2);
			nx+=d;
			ny-=d;
			ans+=d;
		}
		if(dx==-1 && dy==1)
		{
			int d1=nx-1;
			int d2=m-ny;
			int d=min(d1,d2);
			nx-=d;
			ny+=d;
			ans+=d;
		}
		if(dx==-1 && dy==-1)
		{
			int d1=nx-1;
			int d2=ny-1;
			int d=min(d1,d2);
			nx-=d;
			ny-=d;
			ans+=d;
		}
	}
	return 0;
}


// code148C.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <iostream>

using namespace std;
int main()
{
    long long n,m;
    scanf("%I64d %I64d",&n,&m);
    int p=1;
    int z=1000000009;
    for(int i=1;i<=m;i++)
    {
        p*=2;
        if(p>z) p%=z;
    }
    p+=z;
    long long ans=1;
    for(int i=1;i<=n;i++)
    {
        ans*=(p-i);
        ans%=z;
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int mo=int(1e9)+7;

int quickpow(int x,int k) {
  int ret=1;
  while(k>0) {
    if(k&1) ret=1ll*ret*x%mo;
    x=1ll*x*x%mo;
    k>>=1;
  }
  return ret;
}

int n,m;
int main() {
  scanf("%d%d",&n,&m);
  printf("%I64d\n",1ll*quickpow(2*n+2,m)*(n+1-m)%mo*quickpow(n+1,mo-2)%mo);
  return 0;
}


#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define PI1 3.14159265358979311599796346854418516159057617187500

typedef long long int ll;

int main()
{
    //FIO
    int t;
    //cin >> t;
    t=1;
    while(t--)
    {
        ll a,b,c,d;
        scanf("%lld %lld %lld %lld",&a,&b,&c,&d);

        ll rn1 = b-a+1;
        ll rn2 = c-b+1;
        ll rn3 = d-c+1;

        ll ans = rn1*rn2*rn3;
        ll pre[1000010]={0};
        for(int i=a;i<=b;i++)
        {
            pre[i+b]++;
            pre[i+c+1]--;
        }

        for(ll i=1;i<=d+1;i++)
            pre[i] += pre[i-1];

        for(ll i=1;i<=d;i++)
            pre[i] += pre[i-1];

        for(ll i=c;i<=d;i++)
            ans -= pre[i];

        printf("%lld",ans);

    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n,x_floor,sum,diff_1,diff_2,diff_3,add=0;
    cin>>n;

    int arr[n];

    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }

    for(int i=1;i<=n;i++)
    {
        int total=0,mini=INT_MAX;
        for(int j=1;j<=n;j++)
        {
        diff_1=abs(j-i)*2;
        diff_2=abs(i-1)*2;
        diff_3=(j-1)*2;
        sum=diff_1+diff_2+diff_3;

        //cout<<sum*arr[i-1]<<" ";

        total=sum*arr[i-1];

        mini=min(mini,total);

        }
        add=add+mini;
    }

    cout<<add<<"\n";

    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll mod = 998244353;
const ll inv2 = 499122177;
int main(){
	int n;scanf("%d",&n);
	ll dp1=inv2,dp2=inv2*inv2%mod;
	if (n==1){
		printf("%lld\n",dp1);
		return 0;
	}
	for (int i=3;i<=n;++i){
		dp1 = inv2*((inv2*dp1%mod+dp2)%mod)%mod;
		swap(dp1,dp2);
	}printf("%lld\n",dp2);
}


#include<bits/stdc++.h>
#define ll long long
#define mkp make_pair
#define inf 0x3f3f3f3f
using namespace std;
const int mod=1e9+7;
int a[100005];
ll qpow(ll a,ll b)
{
	ll sum=1;
	while(b)
	{
		if(b&1)sum=(sum*a)%mod;
		a=a*a%mod;
		b>>=1;
	}
	return sum;
}
int main()
{
	ll n,m,k,r,c;
	scanf("%lld%lld%lld%lld%lld",&n,&m,&k,&r,&c);
	ll ax,ay,bx,by;
	scanf("%lld%lld%lld%lld",&ax,&ay,&bx,&by);
	ll sum=1;
	if(ax>bx)
	{
		swap(ax,bx);
		swap(ay,by);
	}
	if(ay>by)
	{
		ay=m-ay-c;
		by=m-by-c;
	}
	//printf("%lld %lld %lld %lld\n",ax,bx,ay,by);
	if(ax==bx&&ay==by)
	{
		printf("%lld\n",qpow(k,n*m));
	}
	else
	{
		printf("%lld\n",qpow(k,n*m-r*c));
	}
 }


#include "bits/stdc++.h"
using namespace std;
#define all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 1e5+1, oo = 1e9;
const long long MD = 998244353;
template<long long MOD=MD> struct mdint {
    int d=0;
    mdint () {d=0;}
    mdint (long long _d) : d(_d%MOD){
        if(d<0) d+=MOD;
    };
    friend mdint& operator+=(mdint& a, const mdint& o) {
        a.d+=o.d; if(a.d>=MOD) a.d-=MOD;
        return a;
    }
    friend mdint& operator-=(mdint& a, const mdint& o) {
        a.d-=o.d; if(a.d<0) a.d+=MOD;
        return a;
    }
    friend mdint& operator*=(mdint& a, const mdint& o) {
        return a = mdint((ll)a.d*o.d);
    }
    mdint operator*(const mdint& o) const {
        mdint res = *this;
        res*=o;
        return res;
    }
    mdint operator+(const mdint& o) const {
        mdint res = *this;
        res+=o;
        return res;
    }
    mdint operator-(const mdint& o) const {
        mdint res = *this;
        res-=o;
        return res;
    }
    mdint operator^(long long b) const {
        mdint tmp = 1;
        mdint power = *this;
        while(b) {
            if(b&1) {
                tmp = tmp*power;
            }
            power = power*power;
            b/=2;
        }
        return tmp;
    }
    friend mdint operator/=(mdint& a, const mdint& o) {
        a *= (o^(MOD-2));
        return a;
    }
    mdint operator/(const mdint& o) {
        mdint res = *this;
        res/=o;
        return res;
    }
    bool operator==(const mdint& o) { return d==o.d;}
    bool operator!=(const mdint& o) { return d!=o.d;}
    friend istream& operator>>(istream& c, mdint& a) {return c >> a.d;}
    friend ostream& operator<<(ostream& c, const mdint& a) {return c << a.d;}
};
using  mint = mdint<MD>;
int main() {
    int n; cin >> n;
    n++;
    vector<mint> dp(n+1),dp2(n+1);
    // dp[n] = number of non isomorphic binary trees with depth <= n
    // dp2[n] = number of non isomorphic binary trees with max depth = n.
    // depth = max number of nodes of any root to leaf path in the binary tree.
    dp[0]=1; // empty tree
    dp2[0]=1; // empty tree
    const mint halve = mint(1)/2;
    for(int i=1;i<=n;++i) {
        dp[i] = 1; // empty tree
        dp[i] += (dp[i-1]*(dp[i-1]+1))*halve; // all combinations of <=i-1 depth trees hanging underneath!
        dp2[i] = dp[i]-dp[i-1];
    }
    // if three trees hanging off root.
    mint one=dp2[n-1]*dp[n-2]*(dp[n-2]+1)/2; // one subtree has maximal depth
    mint two=dp2[n-1]*(dp2[n-1]+1)*halve*dp[n-2]; // two subtrees have depth of n-1
    mint three = dp2[n-1]*(dp2[n-1]+1)*(dp2[n-1]+2)/6; // all 3 subtrees have depth of n-1

    mint ans = (one+two+three)*2 -1;
    // need all trees which don't all connect to root.
    for(int i=1;i<n;++i) {
        // don't count the degenerate tree which is a path on the left side
        ans+=(dp2[i]-1)*(dp2[n-i]-dp2[n-i-1]);
        // make sure the right side is a degree 3 vertex, so no double counting.
    }
    cout << ans << '\n';
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 20;

const int c0[] = { 2, 1, 0, 1, 2 };
const int c1[] = { 6, 7, 8, 7, 6 };
const int di[] = { 1, 1, 0, 0, -1, -1 };
const int dj[] = { -1, 1, 2, -2, -1, 1 };

int dp[1 << MAXN];
char ts[16], s[16][16], a[16][16];

int gao() {
	int mask = 0, po = 0;
	for (int i = 0; i < 5; ++i) {
		for (int j = c0[i]; j <= c1[i]; j += 2) {
			if (s[i][j] == 'O') {
				mask |= (1 << po);
			}
			++po;
		}
	}
	int &ret = dp[mask];
	if (ret == -1) {
		ret = 0;
		for (int i = 0; i < 5 && !ret; ++i) {
			for (int j = c0[i]; j <= c1[i] && !ret; j += 2) {
				if (s[i][j] != 'O')
					continue;
				for (int k = 0; k < 6 && !ret; ++k) {
					int ii = i, jj = j;
					while (ii >= 0 && ii < 5 && jj >= c0[ii] && jj <= c1[ii] && s[ii][jj] == 'O') {
						s[ii][jj] = 'X';
						ii += di[k];
						jj += dj[k];
						if (!gao()) {
							ret = 1;
							break;
						}
					}
					while (ii != i || jj != j) {
						ii -= di[k];
						jj -= dj[k];
						s[ii][jj] = 'O';
					}
				}
			}
		}
	}
	return ret > 0;
}

int main() {
	memset(dp, -1, sizeof(dp));
	for (int i = 0; i < 16; ++i)
		for (int j = 0; j < 16; ++j)
			s[i][j] = '?';
	for (int i = 0; i < 5; ++i) {
		for (int j = c0[i]; j <= c1[i]; j += 2) {
			scanf("%s", ts);
			s[i][j] = ts[0];
		}
	}
	printf("%s\n", gao() ? "Karlsson" : "Lillebror");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(void)
{
    long long int n;
    cin>>n;
    int a=log2(n);
    cout<<(a+1)<<endl;
        return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
const int N = 105;
int a[3], dp[N][N][N];
string s[3];
vector<int> f;
vector<int> computePrefix(string pat)
{
    int m = pat.size();
    vector<int> lp(m);
    for(int i = 1, k = 0; i < m; i++)
    {
        while(k > 0 && pat[k] != pat[i])
            k = lp[k - 1];
        if(pat[k] == pat[i])
            lp[i] = ++k;
        else
            lp[i] = k;
    }
    return lp;
}
int solve(int i, int j, int k)
{
    if(k == a[2])
        return -1e9;
    if(i == a[0] || j == a[1])
        return 0;
    int &x = dp[i][j][k];
    if(x != -1)
        return x;
    int z1 = solve(i + 1, j, k), z2 = solve(i, j + 1, k), z3 = -1e9;
    if(s[0][i] == s[1][j])
    {
        int l = k;
        while(l > 0 && s[2][l] != s[0][i])
            l = f[l - 1];
        if(s[2][l] == s[0][i])
            ++l;
        z3 = 1 + solve(i + 1, j + 1 , l);
    }
    return x = max(z1, max(z2, z3));
}
void print(int i, int j, int k)
{
    if(k == a[2])
        return;
    if(i == a[0] || j == a[1])
        return;
    int z1 = solve(i + 1, j, k), z2 = solve(i, j + 1, k), z3 = -1e9, x = -1e9, l = k;
    if(s[0][i] == s[1][j])
    {
        while(l > 0 && s[2][l] != s[0][i])
            l = f[l - 1];
        if(s[2][l] == s[0][i])
            ++l;
        z3 = 1 + solve(i + 1, j + 1 , l);
    }
    x = max(z1, max(z2, z3));
    if(x == z1)
        return print(i + 1, j, k);
    else if(x == z2)
        return print(i, j + 1, k);
    else
    {
        cout << s[0][i];
        print(i + 1, j + 1, l);
    }
}
int main()
{
    IO;
    for(int i = 0; i < 3; i++)
        cin >> s[i],
        a[i] = s[i].size();
    f = computePrefix(s[2]);
    memset(dp, -1, sizeof dp);
    if(!solve(0, 0, 0))
        return cout << 0, 0;
    print(0, 0, 0);
}


#include <bits/stdc++.h>
using namespace std;
const int ZERO = 151;
const int MAXN = 305;
int maxdep;
int dr[] = {1,1,1,0,0,-1,-1,-1};
int dc[] = {-1,0,1,-1,1,-1,0,1};
int split_dir[][2] = {{1,3},{0,2},{1,4},{0,5},{2,7},{3,6},{5,7},{6,4}};
bool split[MAXN], board[MAXN][MAXN], vis[MAXN][MAXN][MAXN][8];
void recurse(int depth, int r, int c, int dir)
{
	if(depth > maxdep)
		return;
	board[r][c] = true;
	if(vis[r][c][depth][dir])
		return;
	vis[r][c][depth][dir] = true;
	if(split[depth])
	{
		int d1 = split_dir[dir][0], d2 = split_dir[dir][1];
		int r1 = r + dr[d1], c1 = c + dc[d1];
		int r2 = r + dr[d2], c2 = c + dc[d2];
		recurse(depth+1,r1,c1,d1);
		recurse(depth+1,r2,c2,d2);
	}
	else
	{
		recurse(depth+1,r + dr[dir],c + dc[dir],dir);
	}
}
int main()
{
	int n;
	cin>>n;
	for (int i = 0; i < n; ++i)
	{
		int x;
		cin>>x;
		maxdep+=x;
		split[maxdep] = true;
	}
	recurse(1,ZERO,ZERO,1);
	int ans = 0;
	for (int i = 0; i < MAXN; ++i)
	{
		for (int j = 0; j < MAXN; ++j)
		{
			if(board[i][j])
				ans++;
		}
	}
	cout<<ans<<"\n";
	return 0;
}


//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define shandom_ruffle random_shuffle

using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;
const int nax=1000*1007;

int n, m;

int s;
ll t;

ll bylem[nax];

void ruch()
{
	if (s<m)
	{
		s=(s+t)%n;
	}
	else
	{
		s=(s-t)%n;
		if (s<0)
			s+=n;
	}
	t--;
}

int main()
{
	scanf("%d%d", &n, &m);
	scanf("%d%lld", &s, &t);
	s--;
	while(1)
	{
		if (bylem[s])
			t%=(bylem[s]-t);
		bylem[s]=t;
		if (t<=1e6)
		{
			while(t)
				ruch();
			printf("%d\n", s+1);
			return 0;
		}
		for (int i=0; i<n; i++)
			ruch();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define loop(i,b,e) for(int i=b;i<=e;i++)
#define loop2(i,e,b) for(int i=e;i>=b;i--)
#define io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define fi first
#define se second
#define X real()
#define Y imag()
#define cp(a,b)	( (conj(a)*(b)).imag() )
#define length(a) (hypot((a).imag(), (a).real()))
typedef complex<double> point;
typedef long long ll;
typedef unsigned long long ull;
double PI = 3.141592653589793;
const double EPS = 1e-9;
const int N = 1e6 + 5;
const ll mod = 1e9 + 7;
const int oo = 1e9;
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };

ll n;
vector<ll> v;

int main() {
	scanf("%lld", &n);
	for (ll i = 1; i * i <= n; i++) {
		if (n % i == 0) {
			v.push_back(i);
			if (i * i != i) v.push_back(n / i);
		}
	}
	ll mx = -1, mn = 1e18;
	for (int i = 0; i < v.size(); i++) {
		for (int j = 0; j < v.size(); j++) {
			ll c = n / (v[i] * v[j]);
			if (n % (v[i] * v[j]) != 0) continue;
			ll x = (v[i] + 1) * (v[j] + 2) * (c + 2);
			mx = max(mx, x - n);
			mn = min(mn, x - n);
			x = (v[i] + 2) * (v[j] + 1) * (c + 2);
			mx = max(mx, x - n);
			mn = min(mn, x - n);
			x = (v[i] + 2) * (v[j] + 2) * (c + 1);
			mx = max(mx, x - n);
			mn = min(mn, x - n);
		}
	}
	printf("%lld %lld", mn, mx);
}


#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int P = 1e9 + 7;
int phi[N];

void initPhi() {
  for (int i = 2;i < N;i++) {
    if (!phi[i]) {
      for (int k = i;k < N;k += i) {
        if (!phi[k]) phi[k] = k;
        phi[k] = phi[k] / i * (i - 1);
      }
    }
  }
}
vector<int> factor[N];
void initFactor() {
  for (int i = 1; i < N; i++) {
    for (int j = i; j < N; j += i) {
      factor[j].push_back(i);
    }
  }
}

int gcd(int a, int b) {
  return b ? gcd(b, a % b) : a;
}

void solve() {
  initPhi();
  initFactor();

  int n;
  cin >> n;
  int64_t ans = 0;
  for (int c = 1; c < n - 1; c++) {
    for (int d : factor[n - c]) {
      ans += 1LL * c * d / gcd(c, d) * phi[(n - c) / d] % P;
    }
  }
  cout << ans % P << endl;
}

int main(){
  solve();
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 70;
const int MOD = 1000000007;
LL n , t , dp[N][N];
int p = 0;
LL dfs (int l , int c , int limit , LL n) {
    if (l < 0) return c == p;
    if (!limit && dp[l][c] != -1) return dp[l][c];
    int up = limit ? ((n >> l) & 1) : 1;
    LL ret = 0;
    for (int i = 0 ; i <= up ; i ++) {
        ret += dfs (l - 1 , c + i , limit && i == up , n);
    }
    if (!limit) dp[l][c] = ret;
    return ret;
}
LL gao (LL n) {
    int t = 0;
    while ((1LL << t) < n) t ++;
    return dfs (t , 0 , 1 , n);
}
int main(){
#ifndef ONLINE_JUDGE
    freopen ("input.txt" , "r" , stdin);
    // freopen ("output.txt" , "w" , stdout);
#endif
    cin >> n >> t;
    if ((t & (-t)) != t) {
        puts ("0");
        return 0;
    }
    while ((1LL << p) < t) p ++;
    p ++;
    memset (dp , -1 , sizeof (dp));
    cout << gao (n + 1) - gao (1) << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define forn(i, a, n) for (int i = a; i < n; ++i)
#define ford(i, a, n) for (int i = n - 1; i >= a; --i)
#define fore(i, a, n) for (int i = a; i <= n; ++i)
#define all(a) (a).begin(), (a).end()
#define fs first
#define sn second
#define trace(a)\
    for (auto i : a) cerr << i << ' ';\
    cerr << '\n'
#define eb emplace_back

#ifndef M_PI
const ld M_PI = acos(-1.0);
#endif

const ld eps = 1e-9;
const int INF = 2000000000;
const ll LINF = 1ll * INF * INF;
const ll MOD = 1000000007;

const int N = 2017;
ll dp[N][N];
ll sum[N][N];

ll Add(ll x, ll y) {
    x += y;
    if (x >= MOD) x -= MOD;
    return x;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    forn(i, 0, N) dp[0][i] = 0;
    forn(i, 0, N) sum[0][i] = 0;
    forn(i, 1, N) {
        dp[i][2] = Add(sum[i - 1][2], 1);
        forn(j, 3, N)
            dp[i][j] = Add(dp[i][j - 1], sum[i - 1][j]);
        sum[i][0] = dp[i][0];
        forn(j, 1, N)
            sum[i][j] = Add(sum[i][j - 1], dp[i][j]);
    }
    int n, m;
    cin >> n >> m;
    ll ans = 0;
    forn(i, 0, n)
        fore(j, 2, m) {
            ans += (m - j + 1) * dp[i + 1][j] % MOD * (dp[n - i][j] - dp[n - i - 1][j] + MOD);
            ans %= MOD;
        }
    cout << ans << '\n';
}


#pragma comment(linker, "/STACK:167177216")

#include<stdio.h>
#include<stack>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<set>
#include<memory.h>
#include<vector>
#include<map>
#include<queue>
#include<iomanip>

using namespace std;

typedef long long li;
typedef long double ld;

#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define pb push_back
#define mp make_pair

int t2(int a, int b)
{
    int k = 1;
    forn (i, 100)
    {
        k *= a;
        if(k % b == 0)
            return i + 1;
        k %= b;
    }
    return 0;
}

bool t3(int a, int b)
{
    if((a % b) == 1)
        return true;
    return false;
}

bool t11(int a, int b)
{
    if(a % b == 1 && a * a % b == b - 1)
        return true;
    if(a % b == b - 1 && a * a % b == 1)
        return true;
    return false;
}

bool t6(int a, int b)
{
    int a2 = 0, a3 = 0, a11 = 0;
    for (int i = 2; i * i <= b; ++i)
    {
        if(b % i == 0)
        {
            int perm = 1;
            while(b % i == 0)
            {
                perm *= i;
                b /= i;
            }
            bool ok = false;
            if(t2(a, perm))
                ok = a2 = 1;
            if(t3(a, perm))
                ok = a3 = 1;
            if(t11(a, perm))
                ok = a11 = 1;
            if(!ok)
                return false;
        }
    }
    if(b > 1)
    {
        bool ok = false;
        int perm = b;
        if(t2(a, perm))
            ok = a2 = 1;
        if(t3(a, perm))
            ok = a3 = 1;
        if(t11(a, perm))
            ok = a11 = 1;
        if(!ok)
            return false;
    }
    return a2 + a3 + a11 >= 2;
}

int main()
{

    int a, b;
    cin >> a >> b;

    if(t2(a, b))
        cout << "2-type" << endl << t2(a, b) << endl;
    else
        if(t3(a, b))
            cout << "3-type" << endl;
        else
            if(t11(a, b))
                cout << "11-type" << endl;
            else
                if(t6(a, b))
                    cout << "6-type" << endl;
                else
                    cout << "7-type" << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
ll qpow( ll x , ll y)
{
    ll ans = 1;
    x %= mod;
    while( y ){
        if( y & 1 )
            ans = ans * x % mod;
        x = x * x % mod;
        y >>= 1;
    }
    return ans;
}
typedef vector<int> VI;
namespace linear_seq{
    const int n = 10010;
    ll res[n], base[n], c[n], md[n];
    vector < int > Md;
    void mul( ll *a, ll *b, int k){
        for ( int i = 0; i < k + k; i++){
            c[i] = 0;
        }
        for ( int i = 0; i < k; i++){
            if ( a[i] ){
                for (int j = 0; j < k; j++){
                    c[ i + j ]=( c[ i + j ] + a[i] * b[j] ) % mod;
                }
            }
        }
        for (int i = k + k - 1; i >= k; i--){
            if ( c[i] ){
                for (int j = 0; j < (int)Md.size(); j++){
                    c[ i - k + Md[j] ] = ( c[ i - k + Md[j] ] - c[i] * md[ Md[j] ]) % mod;
                }
            }
        }
        for (int i = 0; i < k; i++){
            a[i] = c[i];
        }
    }

    ll solve( ll n, VI a, VI b){
        ll ans = 0, pnt = 0;
        int k = (int)a.size();
        assert( (int)a.size() == (int)b.size() );
        for (int i = 0; i < k; i++){
            md[ k - 1 - i] = - a[i];
        }
        md[k] = 1;
        Md.clear();
        for ( int i = 0; i < k; i++){
            if ( md[i] != 0){
                Md.push_back(i);
            }
        }
        for ( int i = 0; i < k; i++){
            res[i] = base[i] = 0;
        }
        res[0] = 1;
        while ( ( 1ll << pnt ) <= n ){
            pnt++;
        }
        for (int p = pnt; p >= 0; p--){
            mul( res, res, k);
            if ( ( n>>p ) & 1){
                for (int i = k - 1; i >= 0; i--){
                    res[ i + 1 ]=res[i];
                }
                res[0] = 0;
                for (int j = 0; j< (int) Md.size(); j++){
                    res[ Md[j] ]=( res[ Md[j] ]- res[k] * md[ Md[j] ] ) % mod;
                }
            }
        }
        for (int i = 0; i < k; i++){
            ans=( ( ans + res[i] * b[i] ) % mod + mod ) % mod;
        }
        return ans;
    }
    VI BM( VI s){
        VI C( 1 , 1 ), B( 1 , 1 );
        int L = 0, m = 1, b = 1;
        for ( int n = 0; n < (int)s.size(); n++){
            ll d = 0;
            for (int i = 0; i < L + 1; i++){
                d=( d + (ll)C[i] * s[ n - i ] ) % mod;
            }
            if ( d == 0 ){
                m++;
            }
            else if ( 2 * L <= n ){
                VI T = C;
                ll c = mod - d*qpow( b, mod - 2 ) % mod;
                while ( (int)C.size() < (int)B.size() + m){
                    C.push_back(0);
                }
                for ( int i = 0; i < (int)B.size(); i++){
                    C[ i + m ] = ( C[ i + m ] + c * B[ i ] ) % mod;
                }
                L = n + 1 - L;
                B = T;
                b = d;
                m = 1;
            }
            else {
                ll c = mod - d * qpow( b , mod - 2 ) % mod;
                while ( (int)C.size() < (int)B.size() + m ){
                    C.push_back(0);
                }
                for (int i = 0; i < (int)B.size(); i++){
                    C[ i + m ]= ( C[ i + m ] + c * B[ i ] ) % mod;
                }
                m++;
            }
        }
        return C;
    }
    ll gao( VI a, ll n){
        VI c = BM( a );
        c.erase( c.begin() );
        for (int i = 0; i < (int)c.size(); i++){
            c[i]= ( mod - c[i] )%mod;
        }
        return solve( n, c, VI( a.begin(), a.begin() + (int)c.size() ) );
    }
};
ll f[300];
int main()
{
    int m;
    ll n;
    scanf("%lld%d",&n,&m);
    VI a;
    for(int i=1;i<=m;i++){
        f[i]=1;
    }
    for(int i=m;i<=200;i++){
        f[i]=(f[i-1]+f[i-m])%mod;
    }
    for(int i=1;i<=200;i++){
        a.push_back(f[i]);
    }
    n++;
    ll ans=linear_seq::gao(a,n-1);
    printf("%lld\n",ans%mod);
    return 0;
}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(),c.end()
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;
int main()
{
    int  t;

    cin>>t;

    int n=t;

    int p1=1;
    int p2=2;
    int s=3;

   int a[t];

   for(int i=0;i<t;i++) {
   	cin>>a[i];
   }

   if(a[0]==3) {
   	cout<<"NO";
   	return 0;
   }

  for(int i=0;i<t;i++) {
  	if(a[i]!=p1&&a[i]!=p2) {
  		cout<<"NO";
  		return 0;
  	}

    else {
    	p1=a[i];
    	p2=s;
    	s=6-a[i]-s;
    }

  }

  cout<<"YES";

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
typedef int INT;
#define int long long
#define x first
#define y second
typedef pair<int,int> PR;
vector<int> a[20];
#define MK make_pair
#define PB push_back
const int Mx=(1<<16)-1;
int mod=10;
int L=60;
int n;
int mul(int x,int y){
	int ret=0;
	for(int i=0;i<3;i++){
		ret+=(x&Mx)*y%mod;
		x>>=16;
		y=(y<<16)%mod;
	}
	return ret%mod;
}
PR Calc(int x){
	if(x==1)return MK(0,1);
	PR A=Calc(x&1?x-1:x/2);
//	cout<<A.x<<" "<<A.y;
//	puts("fuck");
	if(x&1)return MK(A.y,(A.x+A.y)%mod);
//	puts("fuckstep2");
	return MK((mul(A.x,A.x)+mul(A.y,A.y))%mod,mul(A.x*2+A.y,A.y)%mod);
}
INT main(){
// 	freopen("a.in","r",stdin);
	cin>>n;
	for(int i=1;i<=60;i++)if(Calc(i).x==n%mod) a[0].PB(i);
	for(int t=0;t<12;t++){
		if(a[t].size()==0)break;
		mod=mod*10;
		for(int k=0;k<a[t].size();k++){
			int i=a[t][k];
			for(int j=0;j<=9;j++){
				if(Calc(L*j+i).x==n%mod)a[t+1].PB(L*j+i);
			}
		}
		L=L*10;
	}
	int ans=-1;
	if(a[12].size()==0){
		cout<<ans;
		return 0;
	}
	for(int k=0;k<a[12].size();k++){
		int i=a[12][k];
		if(ans<0)ans=i-1;
		if(i-1<ans)ans=i-1;
	}
	cout<<ans;
	return 0;
}


/*
ID: espr1t
LANG: C++
TASK: Demo
*/

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <queue>
#include <map>
#include <set>

#define MAX 16

using namespace std;
FILE *in; FILE *out;

struct Edge
{
        int lo, hi, prc;
};

int n, all, flow, cur;
int cap[MAX];
Edge edges[MAX][MAX];

void recurse(int node, int next, int price)
{
        if (node == n - 1)
        {
                if (cap[node] == flow)
                        cur = max(cur, price);
                return;
        }

        if (next >= n)
        {
                if (cap[node] == 0)
                        recurse(node + 1, node + 2, price);
        }
        else
        {
                for (int i = edges[node][next].lo; i <= edges[node][next].hi; i++)
                {
                        if (cap[node] < i) break;
                        cap[node] -= i; cap[next] += i;
                        int nprice = price + i * i;
                        if (i > 0) nprice += edges[node][next].prc;
                        recurse(node, next + 1, nprice);
                        cap[node] += i; cap[next] -= i;
                }
        }
}

int eval()
{
        for (int i = 0; i < n; i++)
                cap[i] = 0;
        cap[0] = flow;
        cur = -1;
        recurse(0, 1, 0);
        return cur;
}

int main(void)
{
        in = stdin; out = stdout;
//      in = fopen("C.in", "rt"); out = fopen("C.out", "wt");

        fscanf(in, "%d", &n);
        all = (n * (n - 1)) / 2;
        for (int i = 0; i < all; i++)
        {
                int n1, n2;
                fscanf(in, "%d %d", &n1, &n2);
                n1--; n2--;
                fscanf(in, "%d", &edges[n1][n2].lo);
                fscanf(in, "%d", &edges[n1][n2].hi);
                fscanf(in, "%d", &edges[n1][n2].prc);
        }

        int ans = -1, best = -1;
        for (flow = 0; flow <= 25; flow++)
        {
                best = eval();
//              cout << "Eval with flow = " << flow << " gives " << best << endl;
//              system("pause");
                if (best != -1) {ans = flow; break;}
        }
        fprintf(out, "%d %d\n", ans, best);

        return 0;
}


#include<stdio.h>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
int xp , yp , xv , yv;
int cal(int x){
	return (x >= 0 ? x : -x);
}
main(){
//	freopen("test.inp","r",stdin);
	scanf("%d %d %d %d",&xp,&yp,&xv,&yv);
	if(xv + yv < xp + yp) printf("Vasiliy");
	else{
		int dia1 = xv - yv;
		int dia2 = xp - yp;
		bool can_reach = true;
		int step = cal(dia1 - dia2);
		if(dia1 > dia2 && yp < step) can_reach = false;
		if(dia2 > dia1 && xp < step) can_reach = false;
		if(!can_reach) printf("%s", max(xv , yv) < xp + yp ? "Vasiliy" : "Polycarp");
		else{
			int l1 = xp + yp - step;
			int l2 = xv + yv - (step <= min(xv , yv) ? 2*step : step + min(xv , yv));
			printf("%s",(l1 <= l2) ? "Polycarp" : "Vasiliy");
		}
	}

}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#define LL long long
using namespace std;
LL gao(LL l1,LL r1,LL l2,LL r2)
{
    if(l1==l2&&r1==r2)
        return 0;
    if(r1<=l2)
        return l2-r1+1;
    else
        return l1-r2+1;
}
long long L(LL x,LL m)
{
    if(x>0)
    {
        LL y=x%m;
        return (x-y)/m;
    }
    else
    {
        x=-x;
        LL y=x%m;
        return -(x+(m-y))/m;
    }
}
long long R(long long x,LL m)
{
    if(x>0)
    {
        LL y=x%m;
        return (x+m-y)/m;
    }
    else
    {
        x=-x;
        LL y=x%m;
        return -(x-y)/m;
    }
}
int main()
{
    long long a,b,x1,y1,x2,y2;
    cin>>a>>b>>x1>>y1>>x2>>y2;
    a*=2,b*=2;
    LL u1=R(x1+y1,a);
    LL d1=L(x1+y1,a);
    LL u2=R(x2+y2,a);
    LL d2=L(x2+y2,a);
    LL ans=gao(d1,u1,d2,u2);
    u1=R(y1-x1,b);
    d1=L(y1-x1,b);
    u2=R(y2-x2,b);
    d2=L(y2-x2,b);
    ans=max(ans,gao(d1,u1,d2,u2));
    cout<<ans;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <string>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <cassert>
#include <queue>
#include <utility>
#include <set>
#include <stack>
#include <vector>
#include <map>
#define YOU using
#define DONT namespace
#define SAY std
YOU DONT SAY;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;
typedef pair<int,ll> pil;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<pll> vll;
#define REPP(i,a,b,d) for(int i=a;i<=b;i+=d)
#define REP(i,a,b) REPP(i,a,b,1)
#define REVV(i,a,b,d) for(int i=a;i>=b;i-=d)
#define REV(i,a,b) REVV(i,a,b,1)
#define FOR(i,a) for(int i=0; i<a; i++)
#define FORD(i,a) for(int i=(int)a-1; i>=0; i--)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
const int OO = 1e9;
const ll INF = 1e18;
const int irand(int lo,int hi){
	return ((double)rand()/(RAND_MAX + 1.0)) * (hi-lo+1) + lo;
}
const ll lrand(ll lo,ll hi){
	return ((double)rand()/(RAND_MAX + 1.0)) * (hi-lo+1) + lo;
}
#define getc getchar
template<typename T>
T getnum(){
	int sign = 1;
	T ret = 0;
	char c;
	do{
		c = getc();
	}while(c == ' ' || c == '\n');
	if(c == '-')sign = -1;
	else ret = c-'0';
	while(1){
		c = getc();
		if(c < '0' || c > '9')break;
		ret = 10*ret + c-'0';
	}
	return sign * ret;
}
inline void ini(int& x){
	x = getnum<int>();
}
inline void scani(int& x){
	scanf("%d",&x);
}
int n,m;
int solve(int m, int n){
	if(n == 1)return -1;
    else if(n == 2 && m > 3)return -1;
    else if(n == 3 && m > 5)return -1;
    else if(n == 2 && m == 2)return 5;
    else if(m == 3 && n == 2)return 11;
    else if(n >= 2*m)return 1;
    else if(n > m && n < 2*m)return 3;
    else if(n == 3){
    	if(m == 3)return 5;
    	else if(m == 4)return 9;
    	else if(m == 5)return 11;
	}else{
		int n7 = (n-1)/2;
		int nl = (n-2)/2;

		m -= n;
		m++;
		int curr = 5;
		while(1){
			if(curr == 7)m -= n7;
			else m -= nl;
			if(m <= 0)return curr;
			curr += 2;
		}
	}
}
int main(){
	cin>>m>>n;
    cout << solve(m, n) << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

using ull = unsigned long long;
const int maxn = 111;
int n;
int p[maxn] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
ull ret;

void dfs(int depth, ull cur, int cnt, int hi) {
  if(depth >= 10 || cnt > n) return;
  if(cnt == n) {
    ret = min(ret, cur);
    return;
  }
  for(int i = 1; i <= hi; i++) {
    if(cnt * (i + 1) > n) break;
    cur *= (ull)p[depth];
    dfs(depth+1, cur, cnt*(i+1), i);
  }
}

signed main() {
  // freopen("in", "r", stdin);
  while(~scanf("%d",&n)) {
    ret = 1E18;
    dfs(1, 1, 1, 60);
    cout << ret << endl;
  }
  return 0;
}


//#pragma comment(linker, "/STACK:134217728")

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <memory>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
using namespace std;

typedef long long Int;
typedef pair<int,int> PII;
typedef vector<int> VInt;

#define FOR(i, a, b) for(i = (a); i < (b); ++i)
#define RFOR(i, a, b) for(i = (a) - 1; i >= (b); --i)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define SIZE(a) int((a).size())
#define ALL(a) (a).begin(),(a).end()
#define PB push_back
#define MP make_pair

Int A[1 << 20];

Int f(Int n)
{
	if(n % 3 != 0)
		return 0;

	int i, j;
	n /= 3;
	Int res = 0;
	Int c;
	int cnt = 0;
	for(c = 2; c*c < n; ++c)
		if(n % c == 0)
		{
			A[cnt] = c;
			++cnt;
		}

	FOR(i, 0, cnt)
		FOR(j, 0, i + 1)
		{
			Int r = 0;
			Int ab = A[i];
			Int ac = A[j];
			Int bc = ab - ac;

			Int t = (ab + ac)*(ab + ac) - bc*bc;
			if((4*n) % t == 0)
			{
				bc = 4*n/t;
				Int d = (ab + bc + ac);
				if(d % 2 != 0)
					continue;

				d /= 2;
				Int a = d - bc;
				Int b = d - ac;
				Int c = d - ab;
				if(a <= 0 || b <= 0 || c <= 0)
					continue;

				if(d*d*d - a*a*a - b*b*b - c*c*c == 3*n)
					++r;
			}

			res += r;
			if(i != j)
				res += r;
		}

	//for(c = 2; c*c <= n; ++c)
	//	if(n % c == 0)
	//		for(a = 1; a < c; ++a)
	//		{
	//			b = c - a;
	//			Int A = 1;
	//			Int B = a + b;
	//			Int C = a*b - n/(a + b);
	//			Int D = B*B - 4*A*C;
	//			if(D < 0)
	//				continue;

	//			Int Min = B, Max = 1 << 30;
	//			while(Max - Min > 1)
	//			{
	//				Int Mid = (Max + Min)/2;
	//				if(Mid*Mid > D)
	//					Max = Mid;
	//				else
	//					Min = Mid;
	//			}

	//			if(Min*Min == D)
	//				++res;
	//		}

	return res;
}

int main()
{
	Int n;
	scanf("%lld", &n);

	printf("%lld\n", f(n));
	return 0;
};


#include <cstdio>
#include <algorithm>
using namespace std ;
int solver( int l1 , int r1 , int l2 , int r2 , int step = 30 ){
       if( l1 > r1 || l2 > r2 ) return 0 ;  //结束条件
       int ovl = max( l1 , l2 ) ;
       int ovr = min( r1 , r2 ) ;
       int overlap = ( (ovl <= ovr) ? (ovr - ovl + 1) : 0 ) ; //算出重叠部分
       if( l1 <= l2 && r1 >= r2 || l1 >= l2 && r1 <= r2 ) return overlap ;  //第一种情况，包含的情况，直接返回重叠部分
       int m = ( 1 << ( step - 1 ) ) ;//取中间值
       int res = overlap ;
       res = max( res , solver( min( l1 , m ) , min( r1 , m - 1 ) , min( l2 , m ) , min( r2 , m - 1 ) , step - 1 ) ) ;
       res = max( res , solver( min( l1 , m ) , min( r1 , m - 1 ) , max( l2 , m + 1 ) - m , max( r2 , m ) - m , step - 1 ) ) ;
       res = max( res , solver( max( l1 , m + 1 ) - m , max( r1 , m ) - m , min( l2 , m ) , min( r2 , m - 1 ) , step - 1 ) ) ;
       res = max( res , solver( max( l1 , m + 1 ) - m , max( r1 , m ) - m , max( l2 , m + 1 ) - m , max( r2 , m ) - m , step - 1 ) ) ;
       return res ;
}

int main()
{
       int l1 , l2 , r1 , r2 ;
       while( ~scanf("%d%d%d%d" , &l1 , &r1 , &l2 , &r2 ) )
              printf("%d\n" , solver( l1 , r1 , l2 , r2 ) ) ;
       return 0 ;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
double fmod(double a, double b){
    return a - b * (int)(a / b);
}
double nd(){
    double n;
    scanf("%lf", &n);
    return n;
}

double a , b, m, vx,vy,vz;

double findX(double xx){
    if (xx >= 0){
        double rmd = fmod(xx, a);
        int f1 = (int)(xx / a);
        if (f1 % 2 == 0){
            return rmd;
        }
        else {
            return a - rmd;
        }
    }
    else {
        xx *= -1;
        double rmd = fmod(xx, a);
        int f1 = (int)(xx / a);
        if (f1 % 2 == 0){
            return rmd;

        }
        else {
            return a - rmd;
        }
    }
}
double findY(double yy){
    if (yy >= 0){
        double rmd = fmod(yy, b);
        int f1 = (int)(yy / b);
        if (f1 % 2 == 0){
            return rmd;
        }
        else {
            return b - rmd;
        }
    }
    else {
        yy *= -1;
        double rmd = fmod(yy, b);
        int f1 = (int)(yy / b);
        if (f1 % 2 == 0){
            return rmd;
        }
        else {
            return b - rmd;

        }
    }
}
int main(){
    a = nd();
    b = nd();
    m = nd();
    vx = nd();
    vy = nd();
    vz = nd();

    double times = m / -vy;
    double xx = a / 2.0;
    double zz = 0;
    xx += times * vx;
    zz += times * vz;
    // printf("%f %f\n", xx, zz);
    xx = findX(xx);
    zz = findY(zz);
    printf("%f %f\n", xx, zz);
    return 0;
}


#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
int n,x,a[1000000],tot;
long long modpow(long long x,long long y,int p){
	long long res=1;
	for(;y;y>>=1,(x*=x)%=p)
		if(y&1)(res*=x)%=p;
	return res;
}
bool check(int b,int p){
	for(int i=1;i*i<p;i++)
		if((p-1)%i==0){
			a[tot++]=i;
			if(i*i!=p-1)
				a[tot++]=(p-1)/i;
		}
	sort(a,a+tot);
	for(int i=0;i<tot;i++)
		if(a[i]!=1&&modpow(b,(p-1)/a[i],p)==1)return 0;
	return 1;
}
int main(){
	cin>>n>>x;
	n++;
	if(x==2)return puts("-1"),0;
	for(int i=2;i<n;i++)
		if(n%i==0)return puts("-1"),0;
	for(int i=x-1;i>=1;i--)
		if(check(i%n,n))
			return printf("%d\n",i),0;
	puts("-1");
}


#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <bitset>
#include <deque>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <cassert>
#include <unordered_set>
#include <climits>
#include <iomanip>
#include <unordered_map>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef long long ll;
#define mset(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
typedef vector<ll> vll;
const int maxn = 100000;
vector<ll> primes;
bitset<maxn> isprime;
void init(){
    isprime.reset(); isprime.flip();
    primes.pb(2ll);
    for(ll e = 4; e < maxn; e += 2) isprime[e] = 0;
    for(ll e = 3; e < maxn; e += 2) if(isprime[e]){
        primes.pb(e);
        for(ll f = e*e; f < maxn; f += e) isprime[f] = 0;
    }
}
bool isp(ll n){
    if(n == 2) return true;
    if(n < 2 || !(n&1)) return false;
    for(ll e = 3; e*e <= n; e += 2) if(n%e == 0) return false;
    return true;
}
ll solve(ll index, ll number){
    if(index < 0 || number < primes[index]) return 0ll;
    number /= primes[index];
    ll ans = number;
    for(int e = 0; e < index; e++) ans -= solve(e, number);
    return ans;
}
int main(){
    init();
    ll a, b, k; cin >> a >> b >> k;
    if(!isp(k)) cout << 0 << endl;
    else if(k > maxn) cout << (a <= k && k <= b) << endl;
    else{
        int index = lower_bound(primes.begin(), primes.end(), k) - primes.begin();
        cout << solve(index, b) - solve(index, a-1) << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define MAX 1000003
#define pii pair<ll,ll>
#define VP vector< pii >
#define MOD  998244353
#define mp make_pair
#define pb push_back
#define rep(i,a,b) for(ll (i) = (a); (i) < (b); (i)++)
#define all(v) (v).begin(),(v).end()
#define S(x) scanf("%d",&(x))
#define S2(x,y) scanf("%d%d",&(x),&(y))
#define SL(x) scanf("%lld",&(x))
#define SL2(x) scanf("%lld%lld",&(x),&(y))
#define P(x) printf("%d\n",(x))
#define FF first
#define SS second
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n,m,k;
cin>>n>>m>>k;
vector<vector<ll>>dp(2005,vector<ll>(2005,0));
rep(i,1,n+1)
	dp[i][0]=m;
rep(i,2,n+1)
	{
		rep(j,1,i)
			{
				dp[i][j]=(dp[i-1][j-1]*(m-1)+dp[i-1][j])%MOD;
			}
	}
ll ans=dp[n][k]%MOD;
cout<<ans;
return 0;
}


#include <bits/stdc++.h>
#define re(i, x, y) for (int i = (x); i < (y); ++i)
#define rep(i, x, y) for (int i = (x); i <= (y); ++i)
#define repp(i, x, y, d) for (int i = (x); i <= (y); i += (d))
#define reep(i, x, y) for (int i = (x); i <= (y); i <<= 1)
#define pe(i, x, y) for (int i = (x) - 1; i >= (y); --i)
#define per(i, x, y) for (int i = (x); i >= (y); --i)
#define rep_e(i, u) for (int i = head[(u)]; i; i = e[i].nxt)
#define vi vector<int>
#define vL vector<LL>
#define vii vector<pii>
#define viL vector<piL>
#define vLi vector<pLi>
#define vLL vector<pLL>
#define eb emplace_back
#define pb pop_back
#define mp make_pair
#define pii pair<int, int>
#define piL pair<int, LL>
#define pLi pair<LL, int>
#define pLL pair<LL, LL>
#define lowbit(x) ((x) & (-(x)))
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << x << endl
using namespace std;
typedef unsigned int ui;
typedef long long LL;
typedef unsigned long long ULL;
typedef double db;
#define getchar() (SB == TB && (TB = (SB = BB) + fread(BB, 1, 1 << 16, stdin), SB == TB) ? EOF : *SB++)
char BB[1 << 16], *SB = BB, *TB = BB;
template<typename T> void read(T &n) {
	T w = 1;
	n = 0;
	char ch = getchar();
	for ( ; !isdigit(ch); ch = getchar()) {
		if (ch == '-') {
			w = -1;
		}
	}
	for ( ; isdigit(ch); ch = getchar()) {
		n = n * 10 + (ch & 15);
	}
	n *= w;
}
template<typename T> void chkmn(T &a, const T &b) {
	(a > b) && (a = b);
}
template<typename T> void chkmx(T &a, const T &b) {
	(a < b) && (a = b);
}

int MOD;
int adt(const LL &a) {
	return (a % MOD + MOD) % MOD;
}
int inc(const int &a, const int &b) {
	return a + b >= MOD ? a + b - MOD : a + b;
}
int dec(const int &a, const int &b) {
	return a - b < 0 ? a - b + MOD : a - b;
}
int mul(const int &a, const int &b) {
	return 1LL * a * b % MOD;
}
int sqr(const int &a) {
	return 1LL * a * a % MOD;
}
void Adt(LL &a) {
	a = (a % MOD + MOD) % MOD;
}
void Inc(int &a, const int &b) {
	((a += b) >= MOD) && (a -= MOD);
}
void Dec(int &a, const int &b) {
	((a -= b) < 0) && (a += MOD);
}
void Mul(int &a, const int &b) {
	a = 1LL * a * b % MOD;
}
void Sqr(int &a) {
	a = 1LL * a * a % MOD;
}
int fsp(int a, int x = MOD - 2) {
	int res = 1;
	for ( ; x; x >>= 1, Sqr(a)) {
		if (x & 1) {
			Mul(res, a);
		}
	}
	return res;
}

const int maxn = 5e3 + 5;
int n;
int binom[maxn][maxn], pw[maxn][maxn], fac[maxn], fiv[maxn];

int main() {

    read(n), read(MOD);
    fac[0] = 1;
    rep(i, 1, n) {
        fac[i] = mul(fac[i - 1], i);
    }
    fiv[n] = fsp(fac[n]);
    per(i, n, 1) {
        fiv[i - 1] = mul(fiv[i], i);
    }
    rep(i, 0, n) {
        binom[i][0] = binom[i][i] = 1;
        re(j, 1, i) {
            binom[i][j] = inc(binom[i - 1][j], binom[i - 1][j - 1]);
        }
        pw[i][0] = 1;
        rep(j, 1, n) {
            pw[i][j] = mul(pw[i][j - 1], i);
        }
    }
    int ans = 0;
    re(i, 0, n) {
        re(j, 1, n - i) {
            Inc(ans, mul(mul(binom[n][i], mul(fac[n - i], mul(fiv[j], binom[n - i - 1 - j][j - 1]))), mul(pw[n - i - j][i], pw[n - 1][j])));
        }
    }
    printf("%d\n", ans);
	return 0;
}