#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define MAXN 100000
using namespace std;
typedef long long LL;
vector<int> turns[MAXN+5];//用vector来记录每一个循环
LL k;
int n,m,tcnt=0,chs[MAXN+5],id[MAXN+5],id2[MAXN+5];
//id存的是单次操作之后的状态,id2存的是k次操作之后的操作
LL x[MAXN+5],a[MAXN+5];
LL a2[MAXN+5],x2[MAXN+5];
//a2是之后的差分序列，x2是之后的兔子位置
bool vis[MAXN+5];
int main()
{
//	freopen("rabbit.in","r",stdin);
//	freopen("rabbit.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%lld",&x[i]),id[i]=i;
	for(int i=1;i<=n;i++)
		a[i]=x[i]-x[i-1];
	scanf("%d %lld",&m,&k);
	for(int i=1;i<=m;i++)
		scanf("%d",&chs[i]),swap(id[chs[i]],id[chs[i]+1]);
	for(int i=1;i<=n;i++)
	{
		int p=i;
		if(vis[p]==true)
			continue;
		tcnt++;
		while(vis[p]==false)
		{
			vis[p]=true;
			turns[tcnt].push_back(p);
			p=id[p];
		}
	}
	for(int i=1;i<=tcnt;i++)
	{
		int T=(int)turns[i].size();//对于每一个循环单独计算
		int pos=k%T;
		//处理出开头位置所对应的最终位置，然后向后挪到来求出这个循环里的其他元素所对应的答案
		for(int j=0,p=pos;j<(int)turns[i].size();j++,p=(p+1)%T)
			id2[turns[i][j]]=turns[i][p];
	}
	for(int i=1;i<=n;i++)
		a2[i]=a[id2[i]];
	for(int i=1;i<=n;i++)
		x2[i]=x2[i-1]+a2[i];
	for(int i=1;i<=n;i++)
		printf("%lld.0\n",x2[i]);//因为题目要求，强行加一个.0
	return 0;
}

#include<bits/stdc++.h>
//#include<tr1/unordered_map>
//#include"Bignum/bignum.h"
//#define lll bignum
#define ls(x) ( x << 1 )
#define rs(x) ( x << 1 | 1 )
//#define mid ( ( l + r ) >> 1 )
#define lowbit(x) ( x & -x )
#define debug(x) (cout << "#x = " << x << endl)
#define re register
#define For(i, j, k) for(re int i = (j); i <= (k); i++)
#define foR(i, j, k) for(re int i = (j); i >= (k); i--)
#define Cross(i, j, k) for(re int i = (j); i; i = (k))
using namespace std;
typedef long long ll;
const ll N = 100011;
const ll inf = 0x3f3f3f3f3f3f;

ll n, m, k, x, a[N], b[N], c[N], B[N];
double res = 0;

namespace IO {

	#define dd ch = getchar()
	inline ll read() {
		ll x = 0; bool f = 0; char dd;
		for (; !isdigit (ch); dd) f ^= (ch == '-');
		for (; isdigit (ch); dd)  x = (x << 3) + (x << 1) + (ch ^ 48);
		return f? -x: x;
	}
	#undef dd

	inline void write( ll x ) {
		if ( x < 0 ) putchar ( '-' ), x = -x;
		if ( x > 9 ) write ( x / 10 ); putchar ( x % 10 | 48 );
	}

	inline void wrn ( ll x ) { write (x); putchar ( ' ' ); }

	inline void wln ( ll x ) { write (x); putchar ( '\n' ); }

	inline void wlnn ( ll x, ll y ) { wrn (x), wln (y); }

}

using namespace IO;

inline void Redouble ( ll x ) {
	for (; x; x >>= 1) {
		if (x & 1) {
			For ( i, 1, n ) B[i] = a[b[i]];
			For ( i, 1, n ) a[i] = B[i];
		}
		For ( i, 1, n ) B[i] = b[b[i]];
		For ( i, 1, n ) b[i] = B[i];
	}
}

int main()
{
	n = read();
	For ( i, 1, n ) a[i] = read(), b[i] = i;
	foR ( i, n, 1 ) a[i] = a[i] - a[i - 1];
	m = read(), k = read();
	For ( i, 1, m ) x = read(), swap ( b[x], b[x + 1] );
	Redouble (k);
	For ( i, 1, n )
		res += a[i], printf ("%.1lf\n", res);
	return 0;
}

/*

*/


#include<bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define ll long long
#define rep(i,n) for(int i = 0; i < int(n); i++)
#define vi vector<int>
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template<class T> inline bool chmax(T &a, const T &b){ if(a<b) { a=b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b){ if(b<a) { a=b; return 1; } return 0; }

int main(){
  cin.tie(0), ios::sync_with_stdio(false);
  int k,t; cin >> k >> t;
  int mx = 0;
  vi a(t);
  rep(i,t){
    cin >> a[i];
    chmax(mx,a[i]);
  }
  if((k+1)/2 < mx){
    cout << (mx-(k+1)/2)*2-1;
  }else{
    cout << 0;
  }
  cout << "\n";
  return 0;
}

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

int main()
{
    int h,w;
    cin >> h >> w;
    vector<string> s(h);
    int white_cnt = 0;
    for (int i = 0; i < h; i++)
    {
        cin >> s[i];
        for (int j = 0; j < w; j++)
        {
            if(s[i][j] == '.')white_cnt++;
        }
    }

    int dx[] = {0, 1, 0, -1};
    int dy[] = {1, 0, -1, 0};

    vector<vector<int>> seen(h, vector<int>(w, -1));
    queue<pair<int, int>> que;

    seen[0][0] = 0;
    que.push(make_pair(0, 0));

    while (!que.empty())
    {
        pair<int ,int> pos = que.front();
        int x = pos.first, y = pos.second;
        que.pop();

        for (int i = 0; i < 4; i++)
        {
            int nx = x + dx[i], ny = y + dy[i];

            if(nx < 0 || nx >= h || ny < 0 || ny >= w)continue;
            if(s[nx][ny] == '#')continue;

            if(seen[nx][ny] == -1)
            {
                que.push(make_pair(nx, ny));
                seen[nx][ny] = seen[x][y] + 1;
            }
        }
    }

    if(seen[h - 1][w - 1] == -1)cout << -1 << endl;
    else cout << white_cnt - seen[h - 1][w - 1] - 1 << endl;
}


#include<bits/stdc++.h>
using namespace std;
template <typename T> using vec = vector<T>;

int main()
{
    string s;
    cin >> s;
    smatch smatch;
    if (regex_search(s,smatch,regex("^A[a-z]+C[a-z]+$")))
    {
        cout << "AC" << endl;
    }
    else
    {
        cout << "WA" << endl;
    }
}

#include <stdlib.h>
#include <cmath>
#include <cstdio>
#include <cstdint>
#include <string>
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
using namespace std;
using ll = long long;

int main(){
  string S;
  cin >> S;
  string ans="AC";

  if(S.at(0)!='A') ans="WA";
  if('A'<=S.at(1) && S.at(1)<='Z') ans="WA";
  int count_C=0;
  for(int i=2;i<S.size()-1;++i){
    if('A'<=S.at(i) && S.at(i)<='Z'){
      if(S.at(i)!='C'){
	ans="WA";
	break;
      }
      else{
	if(count_C==0) ++count_C;
	else{
	  ans="WA";
	  break;
	}
      }
    }
  }
  if('A'<=S.at(S.size()-1) && S.at(S.size()-1)<='Z') ans="WA";
  if(count_C!=1) ans="WA";

  cout << ans << endl;

  return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

struct UnionFind{
   vector<int> par;
   UnionFind(int n):par(n,-1){}
   int find(int x){
       if(par[x]<0)return x;
       return par[x]=find(par[x]);
   }
   bool unite(int x,int y){
       x=find(x);
       y=find(y);
       if(x==y)return false;
       if(par[x]>par[y]){
           par[y]+=par[x];
           par[x]=y;
       }
       else{
           par[x]+=par[y];
           par[y]=x;
       }
       return true;
   }

   bool same(int x,int y){
       return find(x)==find(y);
   }
};

vector<pair<ll,pint>> edges;
ll a[202020],d;

ll dist(int x,int y){
    return d*abs(x-y)+a[x]+a[y];
}
void dfs(int l,int r){
    if(l+1>=r)return;
    ll mini=longinf,idx=-1;
    int m=(l+r)/2;
    for(int i=l;i<m;++i){
        if(dist(i,m)<mini)mini=dist(i,m),idx=i;
    }
    for(int i=m;i<r;++i){
        edges.push_back({dist(idx,i),{idx,i}});
    }
    mini=longinf;idx=-1;
    for(int i=m;i<r;++i){
        if(dist(i,l)<mini)mini=dist(i,l),idx=i;
    }
    for(int i=l;i<m;++i){
        edges.push_back({dist(idx,i),{idx,i}});
    }
    dfs(l,m);
    dfs(m,r);
}
int main(){
    int n;cin>>n>>d;
    rep(i,n)cin>>a[i];
    dfs(0,n);
    sort(edges.begin(),edges.end());
    UnionFind uf(n);
    ll ans=0;
    for(auto e:edges){
        if(uf.unite(e.second.first,e.second.second))ans+=e.first;
    }
    cout<<ans<<endl;
    return 0;
}

#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<iostream>
#define N 1100000
using namespace std;
char s[N],t[N];
int p[N],head,tail,n,f[N],ans;
int main()
{
	scanf("%d",&n);
	scanf("%s",s+1);
	scanf("%s",t+1);
	bool bo=1;
	for(int i=1;i<=n;i++) if(s[i]!=t[i]) {bo=0;break;}
	if(bo) {printf("0\n");return 0;}
	int j=n;
	for(int i=n;i>=1;i--)
	{
		j=min(j,i);
		while(j && s[j]!=t[i]) j--;
		if(j==0) {printf("-1\n");return 0;}
		f[i]=j;
	}
	head=1;tail=0;
	for(int i=n;i>=1;i--)
	{
		while(head<=tail && p[head]-(tail-head)>i) head++;
		if(f[i]!=f[i+1]) p[++tail]=f[i];
		ans=max(ans,tail-head+1);
	}
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
//namespace mp = boost::multiprecision;

using namespace std;

const double PI = 3.14159265358979323846;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
//#define rep(i, n) for (ll i = 0; i < (n); ++i)
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

double equal(double a, double b) {
    return fabs(a - b) < DBL_EPSILON;
}

std::istream &operator>>(std::istream &in, set<int> &o) {
    int a;
    in >> a;
    o.insert(a);
    return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
    ll a;
    in >> a;
    o.push(a);
    return in;
}

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);
//const ll mod = 1e10;

typedef priority_queue<ll, vector<ll>, greater<ll> > PQ_ASK;

int main() {
    double n, m, d;
    cin >> n >> m >> d;

    double ans = [&]() -> double {

        if (d == 0) {
            return 1.0 / n * (m - 1);
        } else {
            return 2.0 * (n - d) / n / n * (m - 1);
        }
    }();

    cout << setprecision(20) << ans << endl;
}

#include <algorithm>
#include <iostream>
#include <string>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cctype>

using namespace std;

int main(void) {

    double x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;

    printf("%f\n", sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)));

    return 0;
}


#include <iostream>
#include <stdio.h>
#include <string>
#include <math.h>
#include <algorithm>
using namespace std;
int main(){
	int d, x, y, Y[600];
	int i, sum = 0;

	for(i = 0;i < 600;i++){
		Y[i] = 0;
	}
	while(cin >> d){
		for(i = 1;d*i < 600;i++){
			x = d * i;
			y = x * x;
			Y[i] = y * d;
		}

		for(i = 1;i < 600;i++){
			sum += Y[i];
		}
		cout << sum << endl;
		sum = 0;
	}
	return 0;
}

#include <iostream>

using namespace std;

int main()
{
	int d;

	while (cin >> d)
	{
		cout << 72000000 - 180000 * d + 100 * d * d << endl;
	}

	return 0;
}


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

int main() {
  long K, x;
  string S;
  cin >> K >> S;
  x = S.size();

  if (x > K) S = S.substr(0, K) + "...";

  cout << S << endl;
}

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

int main(){
  int a, b, c, d;
  cin>>a>>b>>c>>d;
  string ans;
  if(a+b>c+d){
    ans="Left";
  }
  else if(a+b<c+d){
    ans="Right";
  }
  else{
    ans="Balanced";
  }
  cout<<ans<<endl;
}


#include<stdio.h>

int main(){

	char word1[105];
	char word2[105];

	scanf ("%s %s", &word1, &word2);
	printf("%s%s\n",word2,word1);

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int gcd(int a, int b) {
  return b != 0 ? gcd(b, a % b) : a;
}//MAX
int lcm(int a, int b) {
   return a * b / gcd(a, b);
   }
//MIN

 main() {
string s;
cin >> s;
for(int i=0;i<s.size()-8;i++){
    cout << s.at(i);
}
cout << endl;
 }


#include "bits/stdc++.h"
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL<<60;
const long long int Mod = 1000000007;
using ll = long long int; using ci = const int;
using vi = vector<int>;  using Vi = vector<long long int>;
using P = pair<int, int>;  using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define rep(i,N) for(ll i = 0; i < (ll)N; i++)
#define repi(i,a,b) for(ll i = ll(a); i < ll(b); ++i)
template<class T>bool chmax(T &former, const T &b) { if (former<b) { former=b; return true; } return false; }
template<class T>bool chmin(T &former, const T &b) { if (b<former) { former=b; return true; } return false; }
template<class T>T sqar(T x){ return x*x; }//sqrt(x)は平方根;
#define Sort(v) std::sort(v.begin(), v.end(), std::greater<decltype(v[0])>()) //降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v> >
template<class T> inline void princ(T x){cout<<x<<" ";};
template<class T> inline void print(T x){cout<<x<<"\n";};
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
///////////////////////////////////////////////////////////////////////////////////

ll n,m;
Vi a;
ll cal(ll n){
	switch(n){
		case 1:
			return 2;
			break;
		case 2:
		case 3:
		case 5:
			return 5;
			break;
		case 4:
			return 4;
			break;
		case 6:
			return 6;
			break;
		case 7:
			return 3;
			break;
		case 8:
			return 7;
			break;
		case 9:
			return 6;
			break;
		default:
			return 0;
			break;
	}
}

vector<string> dp;

int main(){
	cin.tie(0);ios::sync_with_stdio(false);
	std::cout<<std::fixed<<std::setprecision(30);
	cin>>n>>m;
	a.resize(m);
	rep(i,m){
		cin>>a[i];
	}
	dp.resize(n+10,"-1");
	dp[0]="";
	rep(i,n){
		if(dp[i]=="-1")continue;
		rep(j,a.size()){
			ll tmp = i+cal(a[j]);
			string fig = dp[i]+(char)('0'+a[j]);
			string now = dp[tmp];
			if(now=="-1" || now.size()<fig.size()){
				dp[tmp] = fig;
			}else if(now.size()>fig.size()){

			}else{
				chmax(dp[tmp],fig);
			}
		}
	}
	print(dp[n]);
	return 0;
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REPR(i,n) for(int i=n; i>-1; --i)
#define ALL(a) (a).begin(),(a).end()
#define FILL(a,n,x); REP(i,(n)){ (a)[i]=(x); }
#define CINA(a,n); REP(i,(n)){ cin >> (a)[i]; }
#define FILL2(a,n,m,x); REP(i,(n)){ REP(j,(m)){(a)[i][j]=(x);} }
#define CINA2(a,n,m); REP(i,(n)){ REP(j,(m)){cin >> (a)[i][j];} }
#define Liny "Yes\n"
#define Linn "No\n"
#define LINY "YES\n"
#define LINN "NO\n"

#define umap unordered_map
//cout << setfill('0') << right << setw(4) << 12; // "0012"
int keta(ll x){ if(x<10){return 1;} else{return keta(x/10) + 1;}}
int keta_wa(ll x){ if(x<10){return x;} else{return keta_wa(x/10) + x%10;} }
int ctoi(char c){ return ( (c>='0' && c<='9')? c - '0': 0 );}
int __stoi(string s){ return atoi(s.c_str()); }
ll sum(ll a[],ll N){ return accumulate(a,a+N,0LL);}
ll gcd(ll a,ll b){if(a<b)swap(a,b); return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){if(a<b){swap(a,b);} return a/gcd(a,b)*b;}
template<class T> void chmax(T& a, T b){ if(a<b){a=b;} }
template<class T> void chmin(T& a, T b){ if(a>b){a=b;} }

int main(){
  ll inf = pow(10,5);
  // 各数について使うマッチ棒の数を記憶
  umap<int,int> mp;
  mp[1] = 2;
  mp[2] = 5;
  mp[3] = 5;
  mp[4] = 4;
  mp[5] = 5;
  mp[6] = 6;
  mp[7] = 3;
  mp[8] = 7;
  mp[9] = 6;

  int N,M; cin>>N>>M;
  int a[M]; CINA(a,M);
  sort(a,a+M,greater<int>());

  // i本使い切った時の最大桁数をdpで求める
  int dp[N+1]; FILL(dp,N+1,-inf);
  dp[0] = 0LL;
  for(int i=1;i<=N;++i){
    REP(j,M){
      int x = a[j];
      int num = mp[x];
      if(i-num>=0){ chmax(dp[i],dp[i-num]+1); }
    }
  }
  // 上位の桁から順に、条件を満たす最大の数を並べていく
  while(N>0){
    REP(j,M){
      int x = a[j];
      int num = mp[x];
      if( N-num<0){ continue;}
      if( dp[N-num] == dp[N]-1 ){
        cout << x;
        N -= num;
        break;
      }
    }
  }
  cout << "\n";
}


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

int main() {
  int N;
  cin >> N;

  string s;
  map<string, int> S;
  for (int i=0; i<N; i++) {
    cin >> s;
    if (S.count(s)) S.at(s) += 1;
    else S[s] = 1;
  }

  int M;
  cin >> M;
  for (int i=0; i<M; i++) {
    cin >> s;
    if (S.count(s)) S.at(s) -= 1;
  }

  using pis = pair<int, string>;
  priority_queue<pis> cnt;
  for (auto x : S) {  // 要素はキーと値のpair
    cnt.push(pair(x.second, x.first));
  }

  if (cnt.top().first < 0) cout << 0 << endl;
  else cout << cnt.top().first << endl;

  return 0;
}

#define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
#include <cstring>
#include <cmath>
#include <stack>
#include <iomanip>
#include <tuple>
#include <functional>
#include <cfloat>
#include <map>
#include <set>
#include <array>
#include <stdio.h>
#include <string.h>
#include <random>
using ll = long long;
using ull = unsigned long long;
using namespace std;
#define int long long
#define CONTAINS_VEC(v,n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a)+(size))
#define REMOVE(v,a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define ARY_REVERSE(v,a) (reverse((v), (v)+(a)))
#define REP(i, n) for (int (i)=0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
#define CONTAINS_SET(m, a) (m).find((a)) != m.end()
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }

int N;
int M;
map<string, int> S;
map<string, int> T;

signed main()
{
	cin >> N;
	REP(i, N)
	{
		string s;
		cin >> s;
		if (CONTAINS_MAP(S, s))
		{
			S[s]++;
		}
		else
		{
			S[s] = 1;
		}
	}
	cin >> M;
	REP(i, M)
	{
		string s;
		cin >> s;
		if (CONTAINS_MAP(T, s))
		{
			T[s]++;
		}
		else
		{
			T[s] = 1;
		}
	}

	int ma = 0;
	for (auto pair : S)
	{
		int n = pair.second;
		if (CONTAINS_MAP(T, pair.first))
		{
			n -= T[pair.first];
		}
		ma = max(ma, n);
	}
	cout << ma << endl;
}


//#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <time.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second
#define mp make_pair

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() {printf("yes\n");}
void no() {printf("no\n");}
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int N, U, V;
vector <int> Graph[int(1e5 + 5)];
int DistT[int(1e5 + 5)], DistA[int(1e5 + 5)];

int Prepare(){
  scanf("%d%d%d", &N, &U, &V);
  fill(DistT, DistT + N + 1, -1);
  fill(DistA, DistA + N + 1, -1);
  for (int i = 0; i < N - 1; i++){
    int A, B; scanf("%d%d", &A, &B);
    Graph[A].push_back(B);
    Graph[B].push_back(A);
  }
  return 0;
}

int BFS(){
  queue <int> que;
  que.push(U); DistT[U] = 0;
  while (!que.empty()){
    int Q = que.front(); que.pop();
    for (int i = 0; i < Graph[Q].size(); i++){
      int next = Graph[Q][i];
      if (DistT[next] != -1) continue;
      DistT[next] = DistT[Q] + 1;
      que.push(next);
    }
  }
  que.push(V); DistA[V] = 0;
  while (!que.empty()){
    int Q = que.front(); que.pop();
    for (int i = 0; i < Graph[Q].size(); i++){
      int next = Graph[Q][i];
      if (DistA[next] != -1) continue;
      DistA[next] = DistA[Q] + 1;
      que.push(next);
    }
  }
  return 0;
}

int main(){
  Prepare(); BFS();

  int ans = 0;
  for (int i = 1; i <= N; i++){
    if (DistA[i] >= DistA[U] && DistT[i] < DistA[i]){
      ans = max(ans, DistA[i] - 1);
    }
  }
  cout << ans << endl;

  return 0;
}

// ░░░░░░░░( •̪●)░░░░░░░░░░░░░░░░░░░░░░░░
// ░░░░░░███████ ]▄▄▄▄▄▄▄▄▃░░░▃░░░░ ▃░
// ▂▄▅█████████▅▄▃▂░░░░░░░░░░░░░░░░░
// [███████████████████].░░░░░░░░░░░░░░
// ◥⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙◤...░░░░░░░░░░░░

//PointBlank's code (⌐■_■)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

#define repp(I, A, B) for(int I = A; I <= B; I++)
#define rep(I, A, B) for(int I = A; I < B; I++)
#define rrep(I, B, A) for(int I = B; I >= A; I--)
#define pb emplace_back
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define LB lower_bound
#define UB upper_bound
#define MP make_pair
#define mem(A, B) memset(A, B, sizeof(A));
#define mem0(A) memset(A, 0, sizeof(A));
#define quickio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define debug(this) cerr<<"> "<<#this<<" : "<<this<<"\n"
#define bitsf(n) __builtin_popcount(n)
#define in_edges(M) repp(I, 1, M){ll A, B; cin >> A >> B; v[A].pb(B), v[B].pb(A);}
#define in_edges2(M) repp(I, 1, M){ll A, B; cin >> A >> B; v[A].pb(B);}
#define endl "\n"
ll power(ll x, ll y);

const ll MOD = 1000000007;
const int MX = 1e5 + 5;

vector<int> v[MX];

void bfs(vector<int> &level, int s){
	queue<int> q;
	q.push(s);
	while(q.size()){
		auto p = q.front();
		q.pop();
		for(auto i : v[p]) if(!level[i]){
			level[i] = level[p] + 1;
			q.push(i);
		}
	}
}

int main() //PointBlank's code ¯\_(ツ)_/¯
{
	int n, late, early;
	cin >> n >> late >> early;
	in_edges(n - 1);
	vector<int> A(MX, 0), B(MX, 0);
	bfs(A, late);
	bfs(B, early);
	int mx = 1;
	repp(i, 1, n) if(B[i] >= A[i] && B[i] >= mx) mx = B[i];
	for(auto i : v[late]) if(i == early) mx = 1;
	cout << mx - 1;
}

ll power(ll x, ll y)
{
    ll res = 1; x %= MOD;
    while(y > 0){if(y & 1) res = (res*x) % MOD; y = y>>1, x = (x*x) % MOD;}
    return res;
}


/*----------------by syr----------------*/
/*

         iii                  ii
     rBQBBBBBBE       BBR iBBBBQBBL     XBBBBBBQBBBBB
   iBBQJ    7BBB      BBsSBBr   BBQ      i      cBBB
  rBBU       iBBw     BBBQi     HBBi           KBBi
  BBH         BB5    iBBB       isL          wBB5
 GBB         iBBi    6BB                   iBBB
 BBQ         BQB     BBD                  QBBi
 BBB        BQB     iQBi                1BBv
 sBBg     wBBB      QBB               iBBB
  7BBBBBBBBBi       BBR              wBBBBBBBBBBBBB
     irvi           ii               ii    i i iii
                i5U
               BBB
               BB7
              1BB
      iPBBBBBKBBR    JBR1       rQBO   BR  UBQP  iBBQi
    7BBBGs7sXBBBi     QBBr     gBBE   rBB BB2BB7HBZQBB
   QBBi      sBQ       BBB   iBBB     SQBBR  BBBB  cBQ
  gBQ        BBg        BBB KBBi      MBBH   BBB   BBs
 iBBv       iBBi         QBBBL        BBR   pBB   iBB
 pBB        BBB         iBBBB        iBB    BBL   KBB
 MBB       BBBR        BBB JBBi      DBR   iBQ    BBL
 GBB     7BBBB2      PBBH   BBBi     BQr   DBB   iBB
  BQBXwgBBP BB7    1BBB      BBQ7   1BB    BBc   BBB
   2BBBBw   BB    EBBS        QBBi  HBa   iBB    BB7
*/
#include<bits/stdc++.h>

using namespace std;

#define PH push
#define MP make_pair
#define PB push_back
#define fst first
#define snd second
#define FOR(i, x, y) for(int i = (x); i < (y); ++i)
#define REP(i, x, y) for(int i = (x); i <= (y); ++i)
#define x0 x0123456789
#define y0 y0123456789
#define x1 x1234567890
#define y1 y1234567890
#define x2 x2345678901
#define y2 y2345678901
typedef double db;
typedef long long ll;
typedef long double ldb;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9 + 7;
const int maxn = 205;
const int maxm = 1e5 + 5;

int n, x;
int a[maxn], dp[maxn][maxm];

/*
sort them in the increasing order
then for ai (after sorted), if it works,
it must be in front of all the elements that are smaller than it
otherwise it won't cause any influence
so we have dp(i, j) representing if you feed the first i elements with j,
what's the answer:
dp(i, j) = dp(i - 1, j mod a[i]) + dp(i - 1, j) * i
*/

int main(){
	scanf("%d%d", &n, &x);
	FOR(i, 0, n)
		scanf("%d", a + i);
	sort(a, a + n);
	FOR(i, 0, n) FOR(j, 0, maxm){
		if(!i){
			dp[i][j] = j % a[i];
			continue;
		}
		dp[i][j] = (dp[i - 1][j % a[i]] + 1ll * dp[i - 1][j] * i % INF) % INF;
	}
	printf("%d\n", dp[n - 1][x]);
	return 0;
}

#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define F first
#define S second
#define int long long
#define ll long long
//#define int  unsigned long long
#define pb push_back
#define double long double

using namespace std;
using namespace __gnu_pbds;
typedef  tree< int , null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int N = 35000;
const int K = 19;
int mod =  1e9 + 7;
int n, dp[210][100200], a[N], f[N], invf[N];
int solve(int m, int x){
    if (m > n) return dp[m][x] = x;
    if (dp[m][x] != 0) return dp[m][x];
    int cnt = 0;
    int l = m;
    while (l <= n && a[l] > x){
        cnt++;
        l++;
    }
    if (cnt != 0) return dp[m][x] = f[n - m + 1] * invf[n - m - cnt + 1] % mod * solve(m + cnt, x) % mod;
    int res = 0;
    for (int i = m; i <= n; i++){
        res = (res + f[n - m] * invf[n - i] % mod * solve(i + 1, x % a[i])) % mod;
    }
    return dp[m][x] = res;
}
int binpow(int x, int y){
    if (y == 0) return 1;
    if (y % 2) return x * binpow(x, y - 1) % mod;
    int z = binpow(x, y / 2);
    return z * z % mod;
}
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt", "r", stdin);
    int x;
    cin >> n >> x;
    f[0] = 1;
    invf[0] = 1;
    for (int i = 1; i <= n; i++) f[i] = f[i - 1] * i % mod;
    for (int i = 1; i <= n; i++) invf[i] = binpow(f[i], mod - 2);
    for (int i = 1; i <= n; i++) cin >> a[i];
    sort(a + 1, a + n + 1);
    reverse(a + 1, a + n + 1);
    cout << solve(1, x);
}


#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<utility>
#include<cmath>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pi acos(-1)
#define all(v) v.begin(),v.end()
using namespace std;

const double eps = 1e-8;
const double INF = 1e12;

typedef pair<int,int> P;
typedef double Real;
typedef complex<Real> Point;
typedef vector<Point> G;

double cross(const Point& a,const Point& b){//calculate ad-bc(vector product)
	return imag(conj(a)*b);
}

struct L:public vector<Point>{//line data
	L(const Point &a,const Point &b){
		push_back(a);
		push_back(b);
	}
};

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int main(){
	string s[12];
	int dt[12][12];
	while(cin>>s[0]){
		loop(i,1,12)cin>>s[i];
		rep(i,12){
			rep(j,12){
				dt[i][j]=(int)(s[i][j]-'0');
			}
		}
		int ans=0;
		rep(i,12){
			rep(j,12){
				if(dt[i][j]){
					ans++;
					queue<P> que;
					P now(j,i);
					que.push(now);
					while(!que.empty()){
						now=que.front();
						que.pop();
						if(now.second>=12 ||now.first<0 || now.second<0 || now.first>=12)continue;
						if(dt[now.second][now.first]==0)continue;
						dt[now.second][now.first]=0;
						rep(k,4){
							now.first+=dx[k];
							now.second+=dy[k];
							que.push(now);
							now.first-=dx[k];
							now.second-=dy[k];
						}
					}
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define loop(i,a,b) for(ll i=(a);i<ll(b);i++)
#define rep(i,b) loop(i,0,b)

void draw(char g[20][20], int ci, int cj){
    g[ci][cj] = '0';
    int di[] = {0,0,1,-1};
    int dj[] = {1,-1,0,0};
    rep(d,4){
        int ni = ci + di[d];
        int nj = cj + dj[d];
        if(ni >= 12 || nj >= 12 || ni < 0 || nj < 0 || g[ni][nj] == '0') continue;
        draw(g,ni,nj);
    }
}

int solve(char g[20][20]){
    int ans = 0;
    rep(i,12)rep(j,12){
        if(g[i][j] == '1'){
            ++ans;
            draw(g,i,j);
        }
    }
    return ans;
}

int main(){
    char g[20][20];
    while(~scanf("%s",g[0])){
        rep(i,11) scanf("%s",g[i+1]);
        printf("%d\n",solve(g));
    }
}


#include <iostream>
#include <cmath>
using namespace std;

struct Point
{
    double x, y;
    Point(double x=0, double y=0) : x(x), y(y) {}

	double distance (const Point &o) const
	{
        return sqrt((x - o.x) * (x - o.x) + (y - o.y) * (y - o.y));
    }

    Point operator+(const Point &o) const
	{
        return Point(x+o.x, y+o.y);
    }

    Point operator-(const Point &o) const
	{
        return Point(x-o.x, y-o.y);
    }

    Point operator*(const double m) const
	{
        return Point(x*m, y*m);
    }

    Point operator/(const double d) const
	{
        return Point(x/d, y/d);
    }

    double cross(const Point &o) const
	{
        return x * o.y - y * o.x;
    }

    double dot(const Point &o) const
	{
        return x * o.x + y * o.y;
    }

    double atan() const
	{
        return atan2(y, x);
    }

    double norm() const
	{
        return dot(*this);
    }

	double area(const Point &a,const Point &b) const
	{
		double t = (a.x - x) * (b.y - y);
		double t2 = (a.y - y) * (b.x - x);
		double areas = abs(t-t2);
		return  areas/2;
	}
};

int main()
{
	Point p1,p2;
	cin >> p1.x >> p1.y >> p2.x >> p2.y;
	cout << fixed;
	cout.precision(6);
	cout << p1.distance(p2) << endl;

	return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main(){
	pair<double,double> p1,p2;
	cin >> p1.first >> p1.second;
	cin >> p2.first >> p2.second;
	double dis;
	dis=sqrt(pow(p2.first-p1.first,2.0)+pow(p2.second-p1.second,2.0));
	printf("%f\n",dis);
	return 0;
}


#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a), i##_max = (b); i < i##_max; ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()

using namespace std;

constexpr int INF = 1 << 25;
constexpr int MOD = 1000000007;

int main() {
  int n, q;
  cin >> n;
  vector<int> S(n);
  REP(i, n) cin >> S[i];
  cin >> q;

  int cnt = 0, t;
  REP(i, q) {
    cin >> t;
    if (find(ALL(S), t) != S.end()) ++cnt;
  }

  cout << cnt << endl;
}

#include <iostream>
#include <algorithm>
#include <vector>
typedef std::vector<int> TList;

TList Input()
{
    int Count;
    std::cin >> Count;
    TList List(Count);
    for(int i = 0; i < Count; ++i){
        std::cin >> List[i];
    }
    return List;
}
int Count(const TList& S, const TList& T)
{
    int Sum = 0;
    for(TList::const_iterator It = T.begin(); It != T.end(); ++It){
        Sum += std::find(S.begin(), S.end(), *It) != S.end() ? 1 : 0;
    }
    return Sum;
}
int main()
{
    TList S = Input();
    TList T = Input();
    std::cout << Count(S, T) << std::endl;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>
#include <map>
#include <climits>
#include <cassert>
#include <algorithm>
#include <utility>

using namespace std;
using Int = long long int;
using UInt = unsigned long long int;
using VI = vector<long long int>;
using VVI = vector<vector<long long int> >;

int main(void) {
    Int a, b;
    cin >> a >> b;
    cout << a * b << endl;
    return 0;
}

#include<iostream>
#include<string>
#include<vector>
#include<math.h>
#include<algorithm>
#include<cstdio>
#include<stdio.h>
#include<stack>
#include<sstream>
#include<iomanip>
#include<map>
#include<unordered_map>
#include<list>
#include<numeric>
#include <utility>
typedef long long ll;

#define pi 3.14159265358979323846
#define VI vector<int>
#define VLL vector<long long>
#define MAX max_element
#define MIN min_element
#define all(v) v.begin(), v.end()
const ll MOD = 1e9 + 7;
using namespace std;
template<typename T>
T gcd(T a, T b) {
	return b ? gcd(b, a % b) : a;
}
template<typename T>
T lcm(T a, T b) {
	return a * b / gcd(a, b);
}

template<typename T>
T binarysearch(vector<T> A, T key) {
	ll left = 0;ll right = 4;ll mid;
	while (left < right) {
		mid = (left + right) / 2;
		if (key == A[mid]) return 1;
		if (key > A[mid]) left = mid + 1;
		else if (key < A[mid]) right = mid;
	}
	return 0;
}

template<typename T>
T finder(vector<T> vec, T number) {
	auto itr = find(vec.begin(), vec.end(), number);
	size_t index = distance(vec.begin(), itr);
	if (index != vec.size()) { // 発見できたとき
		return 1;
	}
	else { // 発見できなかったとき
		return 0;
	}
}
ll frac(ll n)//階乗
{
	if (n == 0) {
		return 1;
	}
	return (n * frac(n - 1)) % MOD;
}

template<typename T>
T keta(T a) {
	ll kazu = 1;
	while (1) {
		if (a / 10 != 0) { a /= 10;kazu++; }
		else break;
	}
	return kazu;
}//桁数

template<typename T>
T nCr(ll n, T r) {
	ll ans = 1;
	for (int i = n; i > n - r; --i) {
		ans = ans * i;
	}
	for (int i = 1; i < r + 1; ++i) {
		ans = ans / i;
	}
	return ans;
}
/*char r[4];
int yaju[4];
char op[3];
int sum = 0;
scanf("%s", r);
for (int i = 0;i < 4;i++) {
	yaju[i] = r[i] - '0';
}
*///map < string, ll> s;

/*i++ずつずらす
a[0] a[1] a[2]//a[1] a[2] a[3]
a[0] a[2] a[4]//a[1] a[3] a[5]
/*if (N % 2 != 0)a = N / 2;
else a = N / 2 - 1;
for (int i = 1;i <= a;i++) {
	for (int j = 0;j < N - (i * 2);j++) {
		if (S[j] != S[j + i] && S[j + i * 2] != S[j] && S[j + i] != S[j + i * 2]) sum--;
		if (sum == 0)break;
	}
}*/

//mapの使い方
/*map<int, int> mp;
for (int i = 0; i < N; i++)
	mp[a[i]]++;

int ans = 0;
for (auto p : mp) {
	int x = p.first;
	int n = p.second;
	if (n < x) ans += n;
	else ans += n - x;
}*/
//cout << std::fixed << std::setprecision(12) <<<< endl;

int main() {
	ll A,B;
	cin>>A>>B;
	cout<<(A*B)<<endl;
}


#include<iostream>
#include<cstdio>
using namespace std;

int main(){
  struct test{
    int Math;
    int English;
    int Japanese;
  };

  int n;
  double total;
  test student;

  while(1){
    total=0;
    cin >> n;
    if(n==0)break;

    for(int i=0;i<n;i++){
      char rank='A';
      while(1){
	cin >> student.Math >> student.English >> student.Japanese;
	if(student.Math==100||student.English==100||student.Japanese==100)break;
	total=student.Math+student.English;
	total=total/2;
	if(total>=90)break;
	total=student.Math+student.English+student.Japanese;
	total=total/3;
	if(total>=80)break;

	rank='B';
	if(total>=70){
	  break;
	}
	if(total>=50&&(student.Math>=80||student.English>=80))break;

	rank='C';
	if(1)break;
      }
      cout << rank << endl;
    }
  }
  return 0;
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main(void){
	int n;
	while(cin >> n){
		int mat, eng, jap;
		rep(i, n){
			cin >> mat >> eng >> jap;
			int sum = mat + eng + jap;
			if(mat == 100 || eng == 100 || jap == 100 || sum >= 240 || sum-jap >= 180){
				cout << "A" << endl;
				continue;
			}else if(sum >= 210||(sum >= 150 && (mat >= 80 || eng >= 80))){
				cout << "B" << endl;
				continue;
			}else{
				cout << "C" << endl;
			}
		}
	}

	return 0;
}

int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
    #define eprintf(...) 42
#endif

ll n, m;

struct numinfo {
	int digit;
	int match;
	int min_match;
	int n;
};

vector<numinfo> nums;

bool dfs( int i, int remain_match, int remain_keta ){
	if ( i == SZ(nums) ){
		eprintf( "remain=%d %d   ", remain_match, remain_keta );
		rep(k,SZ(nums)){
			eprintf( "%d ", (int)nums[k].n );
		}
		eprintf( "\n" );
		if ( remain_match == 0 && remain_keta <= 0 )
			return true;
		return false;
	}
	for ( int k = remain_match/nums[i].match; k >= 0; k-- ){
		nums[i].n = k;
		int creatable_keta = (remain_match-k*nums[i].match)/nums[i].min_match;
		if ( remain_keta-k-creatable_keta > 0 )
			continue;
		if ( dfs( i+1, remain_match-k*nums[i].match, remain_keta-k ) )
			return true;
	}
	return false;
}

int main(){
	cin >> n >> m;

	vector<int> a(m);
	rep(i,m){
		cin >> a[i];
	}
	sort(a.rbegin(),a.rend());

	int digit_to_match[] = { 0, 2, 5, 5, 4, 5, 6, 3, 7, 6 };
	set<int> s;
	rep(i,m){
		int match = digit_to_match[a[i]];
		if ( s.count(match) == 0 )
			nums.push_back({a[i],match,100,0});
		s.insert(match);
	}
	m = nums.size();

	int min_match = 100;
	for ( int i = nums.size()-1; i >= 0; i-- ){
		nums[i].min_match = min_match;
		chmin(min_match, nums[i].match);
		eprintf( "digit,=%d match=%d min_match=%d\n", nums[i].digit, nums[i].match, nums[i].min_match );
	}

	for ( int keta = n / min_match; keta > 0; keta-- ){
		eprintf( "\n\nketa=%d\n", keta );
		if ( dfs( 0, n, keta ) ){
			sort(nums.begin(), nums.end(), [](numinfo &l, numinfo &r){
				return l.digit>r.digit;
			});
			rep(i,m){
				rep(j,nums[i].n){
					cout << nums[i].digit;
				}
			}
			cout << endl;
			return 0;
		}
	}

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<lint, lint>;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define ALL(a)  (a).begin(),(a).end()
constexpr int MOD = 1000000007;
constexpr int INF = 2147483647;
void yes(bool expr) {
  cout << (expr ? "Yes" : "No") << "\n";
}
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int N, M;
  cin >> N >> M;
  vector<int> A(M);
  REP(i, M) cin >> A[i];
  vector<int> num = {0,2,5,5,4,5,6,3,7,6};
  vector<string> dp(N+1, "x");
  dp[0] = "";
  FOR(i, 1, N+1) {
    REP(j, M) {
      if(i-num[A[j]] >= 0) {
        if(dp[i-num[A[j]]] == "x") continue;
        if(dp[i] == "x" || dp[i-num[A[j]]].size() >= dp[i].size()) {
          dp[i] = dp[i-num[A[j]]] + (char)(A[j] + '0');
        } else if(dp[i-num[A[j]]].size() + 1 == dp[i].size()) {
          dp[i] = max(dp[i], dp[i-num[A[j]]] + (char)(A[j] + '0'));
        }
      }
    }
    //cout << dp[i] << endl;
  }
  cout << dp[N] << endl;
}


#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
#include<stdio.h>
#include<vector>
using namespace std;
#define int long long
#define rep(s,i,n) for(int i=s;i<n;i++)
#define c(n) cout<<n<<endl;
#define ic(n) int n;cin>>n;
#define sc(s) string s;cin>>s;
#define mod 1000000007
#define inf 1000000000000000007
#define f first
#define s second
#define mini(c,a,b) *min_element(c+a,c+b)
#define maxi(c,a,b) *max_element(c+a,c+b)
//printf("%.12Lf\n",);
int keta(int x){
	rep(0,i,30){
		if(x<10){
			return i+1;
		}
		x=x/10;
	}
}
int gcd(int x,int y){
	int aa=x,bb=y;
	rep(0,i,1000){
		aa=aa%bb;
		if(aa==0){
			return bb;
		}
		bb=bb%aa;
		if(bb==0){
			return aa;
		}
	}
}
int lcm(int x,int y){
	int aa=x,bb=y;
	rep(0,i,1000){
		aa=aa%bb;
		if(aa==0){
			return x/bb*y;
		}
		bb=bb%aa;
		if(bb==0){
			return x/aa*y;
		}
	}
}
bool p(int x){
	if(x==1)return false;
	rep(2,i,sqrt(x)+1){
		if(x%i==0&&x!=i){
			return false;
		}
	}
	return true;
}
int max(int a,int b){
	if(a>=b)return a;
	else return b;
}
int min(int a,int b){
	if(a>=b)return b;
	else return a;
}
int n2[41];
int nis[41];
int nia[41];
int mody[41];
int nn;
int com(int n,int y){
	int ni=1;
	for(int i=0;i<41;i++){
		n2[i]=ni;
		ni*=2;
	}
	int bunsi=1,bunbo=1;
	rep(0,i,y)bunsi=(bunsi*(n-i))%mod;
	rep(0,i,y)bunbo=(bunbo*(i+1))%mod;
	mody[0]=bunbo;
	rep(1,i,41){
		bunbo=(bunbo*bunbo)%mod;
		mody[i]=bunbo;
	}
	rep(0,i,41)nis[i]=0;
	nn=mod-2;
	for(int i=40;i>=0;i-=1){
		if(nn>n2[i]){
			nis[i]++;
			nn-=n2[i];
		}
	}
	nis[0]++;
	rep(0,i,41){
		if(nis[i]==1){
			bunsi=(bunsi*mody[i])%mod;
		}
	}
	return bunsi;
}
int yakuwa(int n){
	int sum=0;
	rep(1,i,sqrt(n+1)){
		if(n%i==0)sum+=i+n/i;
		if(i*i==n)sum-=i;
	}
	return sum;
}
bool hei(int n){
	rep(0,i,100){
		if(i*i==n){
			return true;
		}
	}
	return false;
}
int pow(int b,int a){
	int aa=1;
	rep(0,i,a)aa*=b;
	return aa;
}
int a[114514];
int sum[114514];
signed main(){
	ic(n) ic(k)
	c(n-k+1)
}

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pint = pair<int,  int>;
using pll = pair<ll, ll>;
using edge = pair<int, ll>;
using Graph = vector<vector<int>>;
using WeightedGraph = vector<vector<edge>>;
const ll INF = 1LL << 60;
const int INF32 = 1 << 29;
const ll MOD = 1000000007;

int main(){
int a,b; cin >> a >> b;
cout << a-b+1<< endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define all(v) v.begin(), v.end()
bool chmin(ll & a, ll  b) { if (b < a) { a = b; return 1; } return 0; }
bool chmax(ll & a, ll b) { if (b > a) { a = b; return 1; } return 0; }
const ll INF = 999999999999999;
const ll MOD = 1000000007;
const ll MAX_N=500010;
ll a, b, c, d, e, f, p, t, x, y, z, q, m, n, r, h, k, w, l, ans;
int main() {
    cin>>n;
    vl beki;
    multiset<ll> A;
    beki.push_back(1);
    rep(i,40){
        beki.push_back(beki.back()*2);
    }
    rep(i,n){
        cin>>x;
        A.insert(x);
    }

    while(!A.empty()){
        auto ite=A.end();
        ite--;
        x=*ite;
      	A.erase(ite);
        k=upper_bound(all(beki),x*2)-beki.begin()-1;
        if(!A.empty()&&A.count(beki[k]-x)>0){
            auto ite2=A.lower_bound(beki[k]-x);
            A.erase(ite2);
            ans++;
        }
    }
    cout<<ans<<endl;
}

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

int main() {
    ll N;
    cin >> N;
    vector<ll> A(N);
    for (ll i=0; i<N; i++) {
        cin >> A[i];
    }

    string ans = "YES";

    sort(A.begin(),A.end());
    /*
    for (ll i=0; i<N; i++) {
        cout << A[i];
    }
    cout << endl;
    */
    for (ll i=0; i<N-1; i++) {
        if (A[i]==A[i+1]) {
            ans = "NO";
            break;
        }
    }

    cout << ans << endl;

}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 100010001;
const ll LINF = (ll)INF*INF*10;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int k, t;
    cin >> k >> t;
    vi a(t);
    int ma = 0;
    rep(i, t) {
        cin >> a[i];
        ma = max(ma, a[i]);
    }

    if(ma <= k/2) {
        cout << 0 << endl;
    } else {
        if(k&1) {
            cout << ma-1-(k-ma) << endl;
        } else {
            cout << ma-1-(k-ma) << endl;
        }
    }
}

#include <bits/stdc++.h>
#define pb push_back
#define Int long long
using namespace std;

const int MAXN=500005;
const long MOD=(long)(1e9+7);
vector<int> adj[MAXN];
vector<pair<int,int>> G[MAXN];

Int N,M,K;
Int B,C,H,W;
Int X,Y,Z;
vector<Int> A;

Int ceilS(Int x,Int y){
    return (x+y-1)/y;
}
using D=long double;

Int perform(Int x){
    return (x*(x+1))/2LL;
}

int main()
{
        cin>>N>>M;
        --M;--N;
        cout<<ceilS(M,N)<<"\n";

}


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

int main() {
    int n;
    int k;
    int numShow = 1;
    int minNumber = 9999999;
    cin >> n >> k;

    for (int i = 0; i < n; i++) {
        // 操作Aの処理
        for (int a = i; a < n; a++) {
            numShow *= 2;
            //cout << "a";
            // if (numShow > minNumber) {
            //     break;
            // }
        }

        // 操作Bの処理
        for (int b = n - i; b < n; b++) {
            numShow += k;
            //cout << "b";
            // if (numShow > minNumber) {
            //     break;
            // }
        }

        //cout << numShow << endl;
        if (numShow < minNumber) {
            minNumber = numShow;
        }

        numShow = 1;
    }

    cout << minNumber << endl;
}

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

int main() {
  int N,K;
  int A=1;
    cin>>N>>K;
  for(int i=0;i<N;i++) {
    if (A<K)
      A*=2;
    else
      A+=K;
  	}
  cout<<A<<endl;
  }


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

  int cnt = 0;
  while (1) {
    rep(i, n) {
      if (a[i] % 2 != 0) {
        cout << cnt << endl;
        return 0;
      }
      a[i] *= 0.5;
    }
    cnt++;
  }

  return 0;
}

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

int main()
{
    vector<int> a(5);
    rep(i, 5) cin >> a[i];
    sort(a.begin(), a.end());
    int mx = a[4], mn = a[0];
    int k;
    cin >> k;
    if (abs(mx - mn) <= k)
    {
        cout << "Yay!" << endl;
    }
    else
        cout << ":(" << endl;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;

struct data_t {
	string name;
	double eff;

	bool operator<(const data_t& right) const {
		if (eff == right.eff) {
			return ( name < right.name ) || ( name == right.name );
		}
		else {
			return ( eff > right.eff );
		}
	}
};

int main(void) {
	int N;
	cin >> N;

	while ( N != 0 ) {
		vector<data_t> LS(N);
		vector<int> T(N,0);
		int p,temp,d,e,f,s,m;
		for (int i = 0; i < N; i++) {
			cin >> LS[i].name;
			cin >> p;
			for (int j = 0; j < 3; j++) {
				cin >> temp;
				T[i] += temp;
			}
			cin >> d >> e >> f >> s >> m;
			T[i] += m * (d+e);
			LS[i].eff = (double)((m*f*s)-p)/T[i];
		}

		std::sort(LS.begin(), LS.end());

		for (int i = 0; i < N; i++) {
			cout << LS[i].name << endl;
		}
		cout << "#" << endl;
		cin >> N;
	}

	return 0;
}

#include<iostream>
#include<cstdio>
using namespace std;
	int u[150][150],p,v[150][150],xg,yg,q,dx[]={1,0,-1,0},dy[]={0,1,0,-1},w,h;
void ret(int x,int y){
	int a,b,i;
	v[y][x]=1;
	if(xg==x&&yg==y)
		p++;
	else
	for(i=0;i<4;i++){
		a=x+dx[i];
		b=y+dy[i];
		if(a<1||a>w||b<1||b>h||v[b][a]==1)
			continue;
		else if(u[b][a]==q)
			ret(a,b);
	}
}
int main(){
	int xs,ys,c,d,x,y,n;
	while(cin>>w>>h,w||h){
		for(int i=1;i<=h;i++)
			for(int j=1;j<=w;j++)
			v[i][j]=0;
		scanf("%d %d",&xs,&ys);
		scanf("%d %d",&xg,&yg);
		scanf("%d",&n);
		for(int k=0;k<n;k++){
			scanf("%d %d %d %d",&c,&d,&x,&y);
			if(d==0)
				for(int i=0;i<2;i++)
				for(int j=0;j<4;j++)
				u[i+y][j+x]=c;
			else
				for(int i=0;i<4;i++)
				for(int j=0;j<2;j++)
				u[i+y][j+x]=c;
		}
		q=u[ys][xs];
		p=0;
		ret(xs,ys);
		if(p>0)
			printf("OK\n");
		else printf("NG\n");
	}
	return 0;
}


#include<iostream>
#include<iomanip>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<utility>
#include<numeric>
#include<algorithm>
#include<cmath>
#include<climits>
using namespace std;

using ll = long long;

class Stream{
public:
    Stream(){

    }
    //配列以外の入力を受け取る
    void read(){

    }
    template<typename First, typename... Rest>
    void read(First& first, Rest&... rest){
        cin >> first;
        read(rest...);
    }
    //配列を区切って出力する
    template<typename T>
    void write_vec(vector<T>& v, char divider){
        for(size_t i = 0; i < v.size(); i++){
            cout << v[i] << divider;
        }
    }
};

int main(){
    Stream io;

    int R, G;
    io.read(R, G);
    cout << G + G - R;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
  char S;
  int mix=0;
  int a=0;
  for(int i=0; i<3;i++){
    cin >> S;
    if (S=='R'){
      a++;
      mix=max(a,mix);
    } else {
      a=0;
    }
  }
  cout << mix << endl;
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define bit(n) (1<<(n))
#define ii(x) int x; cin >> (x)
#define ii2(x, y) int x, y; cin >> (x) >> (y)
#define ii3(x, y, z) int x, y, z; cin >> (x) >> (y) >> (z)
#define yn(x, ok, ng) cout << ((x)?(ok):(ng)) << endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1;} return 0;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1;} return 0;}
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
int const INF = 1001001001;

struct BipartiteMatching {
  vector< vector< int > > graph;
  vector< int > match, alive, used;
  int timestamp;

  BipartiteMatching(int n) : graph(n), alive(n, 1), used(n, 0), match(n, -1), timestamp(0) {}

  void add_edge(int u, int v) {
    graph[u].push_back(v);
    graph[v].push_back(u);
  }

  bool dfs(int idx) {
    used[idx] = timestamp;
    for(auto &to : graph[idx]) {
      int to_match = match[to];
      if(alive[to] == 0) continue;
      if(to_match == -1 || (used[to_match] != timestamp && dfs(to_match))) {
        match[idx] = to;
        match[to] = idx;
        return true;
      }
    }
    return false;
  }

  int bipartite_matching() {
    int ret = 0;
    for(int i = 0; i < graph.size(); i++) {
      if(alive[i] == 0) continue;
      if(match[i] == -1) {
        ++timestamp;
        ret += dfs(i);
      }
    }
    return ret;
  }

  void output() {
    for(int i = 0; i < graph.size(); i++) {
      if(i < match[i]) {
        cout << i << "-" << match[i] << endl;
      }
    }
  }
};

int main()
{
cin.tie(0);
ios::sync_with_stdio(false);

int n; cin >> n;
vi a(n),b(n),c(n),d(n);
rep(i,n) cin >> a[i] >> b[i];
rep(i,n) cin >> c[i] >> d[i];

BipartiteMatching bm(2*n);

rep(i,n) {
    rep(j,n) {
        if(a[i] < c[j] && b[i] < d[j]) bm.add_edge(i, n+j);
    }
}
cout << bm.bipartite_matching() << endl;
return 0;
}

#include<bits/stdc++.h>
#define fast ios::sync_with_stdio(false), cin.tie(0);
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define endl '\n'

using namespace std;

main()
{
    fast;
    int n,a,b,i;
    cin>>n;
    vector<pair<pair<int,int>,int>> pts;
    for (i=0;i<n;i++)
    {
        cin>>a>>b;
        pts.pb(mp(mp(a,b),0));
    }
    for (i=0;i<n;i++)
    {
        cin>>a>>b;
        pts.pb(mp(mp(a,b),1));
    }
    sort(pts.begin(),pts.end());
    set<pair<int,int>> s;
    int ans=0,mark[2*n]={0};
    for (i=0;i<2*n;i++)
    {
        if (pts[i].ss==0)
            s.insert(mp(pts[i].ff.ss,i));
        else
        {
            int choose=-1;
            for (auto e: s)
            {
                if (e.ff<pts[i].ff.ss && mark[e.ss]==0)
                    choose=e.ss;
            }
            if (choose!=-1)
            {
                ans++;
                mark[i]=1;
                mark[choose]=1;
            }
        }
    }
    cout<<ans<<endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#define MAX 2000200
#define MIN -2000000300
#define INF 1<<30
using namespace std;
int n;
int A[1000][1000];
int d[1000];
bool used[1000];

void Init(int n){
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      A[i][j] = INF;
    }
  }
}

int prime(){
  for(int i=0;i<n;i++){
    d[i] = INF;
    used[i]=false;
  }
  int ans=0;
  d[0] = 0;

  while(true){
    int v = -1;
    for(int i=0;i<n;i++){
      if(!used[i] && (v == -1 || d[v] > d[i]))v = i;
    }
    if(v == -1)break;
    ans+=d[v];
    used[v] = true;

    for(int i=0;i<n;i++){
      d[i] = min(d[i],A[v][i]);
    }

  }
  return ans;
}

int main(){
  scanf("%d",&n);
  //  Init(n);
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      scanf("%d",&A[i][j]);
      if(A[i][j] == -1)A[i][j] = INF;
    }
  }

  printf("%d\n",prime());

  return 0;
}

#include <algorithm>
#include <iostream>
using namespace std;

int P[10010];
void init(int N){
  for (int i=0; i<N; i++) P[i] = i;
}
int root (int a){
  if (P[a] == a) return a;
  else {
    P[a] = root(P[a]);
    return P[a];
  }
}
bool is_same_set(int a, int b){
  return root(a) == root(b);
}
void unite(int a, int b){
  P[root(a)] = root(b);
}

int main(){
  int N;
  cin >> N;
  pair <int, int> edge[10010];
  for (int i=0; i<N; i++){
    for (int j=0; j<N; j++){
      cin >> edge[i*N+j].first;
      edge[i*N+j].second = i*N+j;
    }
  }
  sort(edge, edge+N*N);
  init(N);
  int ans = 0;
  int b = 0;
  for (int k=0; k<N*N; k++){
    while(edge[k].first < 0) k++;
    int i = edge[k].second / N;
    int j = edge[k].second % N;
    if (is_same_set(i,j)){}
    else{
      unite(i,j);
      ans += edge[k].first;
    }
  }
  cout << ans << endl;
}


#include <iostream>
#include<map>
using namespace std;
const int maxn = 2e6 + 10;
typedef pair<int, int> P;
int fa[maxn], ma[maxn];
void init(int n)
{
	for (int i = 0; i <= n; ++i)
	{
		fa[i] = i;
		ma[i] = i;
	}
}
int find(int* fa, int x)//找到根节点
{
	if (fa[x] == x)
	{
		return fa[x];
	}
	return fa[x] = find(fa, fa[x]);
}
void unin(int a, int b, int* fa)
{
	a = find(fa, a);//找到a根节点
	b = find(fa, b);//找到b跟节点
	fa[a] = b;//a为b的父节点
}
map<P, int>ans;
int main()
{
	int n, k, l;
	while (scanf("%d%d%d", &n, &k, &l) != EOF)
	{
		init(n);
		ans.clear();
		for (int i = 0; i < k; ++i)
		{
			int a, b;
			scanf("%d%d", &a, &b);
			unin(a, b, ma);
		}
		for (int i = 0; i < l; ++i)
		{
			int a, b;
			scanf("%d%d", &a, &b);
			{
				unin(a, b, fa);
			}
		}
		for (int i = 1; i <= n; ++i)
		{
			ans[make_pair(find(ma, i), find(fa, i))]++;
		}
		for (int i = 1; i <= n; ++i)
		{
			i == n ? printf("%d\n", ans[make_pair(find(ma, i), find(fa, i))]) : printf("%d ", ans[make_pair(find(ma, i), find(fa, i))]);
		}
	}
	return 0;
}

#include <algorithm>
#include <cstdio>
#include <map>
#include <set>
#include <vector>

int main() {
    int n;
    while (~scanf("%d", &n)) {
        if (n == 0) break;

        std::vector<unsigned int> v;
        std::map<unsigned int, unsigned long long int> m;

        unsigned int a;
        unsigned long long int b, c;
        for (int i = 0; i < n; ++i) {
            scanf("%u %llu %llu", &a, &b, &c);
            v.push_back(a);
            m[a] += b * c;
        }

        std::set<unsigned int> dup;
        bool found = false;
        for (auto i : v) {
            if (m[i] >= 1000000 && dup.count(i) == 0) {
                printf("%u\n", i);
                dup.insert(i);
                found = true;
            }
        }
        if (!found) {
            printf("NA\n");
        }
    }
}


#include <cstdio>
typedef long long lint;
int const N=1e5+10;
int n,a[N];
int gcd(int x,int y)
{
    if(x==-1 || y==-1) return -x*y;
    if(x==0) return y;
    else return gcd(y%x,x);
}
int g1[N],g2[N];
int main()
{
    scanf("%d",&n);
    lint sum=0;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]),sum+=a[i];
    if(n%2==0)
    {
        if(sum%2==1) printf("First");
        else printf("Second");
        return 0;
    }
    if(sum%2==0) {printf("First"); return 0;}
    int cur=0;
    while(true)
    {
        bool flag=false;
        sum=0;
        for(int i=1;i<=n;i++) sum+=a[i];
        g1[0]=-1; g2[n+1]=-1;
        for(int i=2;i<=n;i++) g1[i]=gcd(g1[i-1],a[i]);
        for(int i=n-1;i>=1;i--) g2[i]=gcd(g2[i+1],a[i]);
        int g;
        for(int i=1;i<=n&&!flag;i++)
        {
            if(a[i]==1) continue;
            g=gcd(gcd(g1[i-1],g2[i+1]),a[i]-1);
            if(((sum-1)/g)%2==1) flag=true;
        }
        if(flag)
            for(int i=1;i<=n;i++) a[i]/=g;
        else
        {
            if(cur==0) printf("Second");
            else printf("First");
            return 0;
        }
        cur^=1;
    }
    return 0;
}

#include <cstdio>
#include <cstring>
#include <algorithm>

inline int read()
{
	int data = 0, w = 1;
	char ch = getchar();
	while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
	if (ch == '-') w = -1, ch = getchar();
	while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();
	return data * w;
}

const int N(100010);
int a[N], cnt[2], n;

void trans()
{
	for (int i = 1; i <= n; i++) if (a[i] & 1) --a[i]; int g = a[1];
	for (int i = 2; i <= n; i++) g = std::__gcd(g, a[i]);
	for (int i = 1; i <= n; i++) a[i] /= g;
}

int check()
{
	cnt[0] = cnt[1] = 0;
	for (int i = 1; i <= n; i++) ++cnt[a[i] & 1];
	if (cnt[0] & 1) return 1; else if (cnt[1] > 1) return 0;
	else
	{
		for (int i = 1; i <= n; i++) if (a[i] == 1) return cnt[0] & 1;
		return trans(), check() ^ 1;
	}
}

int main()
{
	n = read();
	for (int i = 1; i <= n; i++) a[i] = read();
	puts(check() ? "First" : "Second");
	return 0;
}


#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
const LL MOD = 1e9 + 7;
LL dp[10005][105][2], D;
vector<int> arr;
LL solve(int i, int d, int op) {
    if (i == -1) return d == 0;
    if (~dp[i][d][op]) return dp[i][d][op];
    dp[i][d][op] = 0;
    int up = op ? arr[i] : 9;
    for (int k = 0 ; k <= up ; k++)
        (dp[i][d][op] += solve(i - 1, (d + k) % D, op && (k == arr[i]))) %= MOD;
    return dp[i][d][op];
}
void marmot0814() {
    string s; cin >> s >> D;
    for (auto &c : s)
        arr.push_back(c - '0');
    reverse(arr.begin(), arr.end());
    memset(dp, -1, sizeof(dp));
    cout << (solve(arr.size() - 1, 0, 1) + MOD - 1) % MOD << '\n';
}
int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int t = 1, kase = 0; // cin >> t;
    while (t--) {
        // cout << "Case #" << ++kase << ":";
        marmot0814();
    }
}

#include <cstdio>
#include <algorithm>

using i8 = std::int8_t;
using u8 = std::uint8_t;
using i16 = std::int16_t;
using u16 = std::uint16_t;
using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using usize = std::size_t;

i32 a[200000];

auto main() -> i32 {
	i32 n, k;
	scanf("%d%d", &n, &k);
	for (i32 i = 0; i < n; i += 1) {
		scanf("%d", a + i);
	}
	i32 b[200001];
	for (i32 step = 0; step < std::min(64, k); step += 1) {
		std::fill(b, b + n + 1, 0);
		for (i32 i = 0; i < n; i += 1) {
			b[std::max(0, i - a[i])] += 1;
			b[std::min(n, i + a[i] + 1)] -= 1;
		}
		a[0] = b[0];
		for (i32 i = 1; i < n; i += 1) {
			b[i] += b[i - 1];
			a[i] = b[i];
		}
	}
	for (i32 i = 0; i < n; i += 1) {
		printf("%d%c", a[i], i == n - 1 ? '\n' : ' ');
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
int main(){
  int N;
  cin >> N;
  string S;
  cin >> S;
  string T;
  char c = 'W';
  for (int i = 0; i < N * 2; i++){
    if (S[i] != c){
      T += 'L';
      c = 'B' + 'W' - c;
    } else {
      T += 'R';
      c = 'B' + 'W' - c;
    }
  }
  int cnt = 0;
  for (int i = 0; i < N * 2; i++){
    if (T[i] == 'L'){
      cnt++;
    }
  }
  if (cnt != N){
    cout << 0 << endl;
  } else {
    int L = 0;
    long long ans = 1;
    for (int i = 0; i < N * 2; i++){
      if (T[i] == 'L'){
        L++;
      } else {
        ans *= L;
        ans %= MOD;
        L--;
      }
    }
    for (int i = 1; i <= N; i++){
      ans *= i;
      ans %= MOD;;
    }
    cout << ans << endl;
  }
}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<vector>
#include<functional>
#include<algorithm>
#include<stdlib.h>
#include<string>
#include<string.h>
#define _USE_MATH_DEFINES
#include<math.h>
#include<deque>
#include<set>
#include<map>
#include<queue>
#include<list>
#include<iostream>
#include <bitset>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(auto i=a;i<b;i++)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x, std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
#define INF 1 << 30
const int mod = 1000000007;
ll power(ll x, ll p) { ll a = 1; while (p > 0) { if (p % 2 == 0) { x *= x; p /= 2; } else { a *= x; p--; } }return a; }
ll mpower(ll x, ll p) { ll a = 1; while (p > 0) { if (p % 2 == 0) { x = x * x % mod; p /= 2; } else { a = a * x % mod; p--; } }return a; }
ll ac(ll n, ll k) { ll a = 1; rep(i, 1, k) { a *= n - i + 1; a /= i; }return a; }
ll mc(ll n, ll m) { ll k = 1, l = 1; rep(i, n - m + 1, n + 1) k = k * i % mod; rep(i, 1, m + 1) l = l * i % mod; l = mpower(l, mod - 2); return k * l % mod; }
typedef struct {
	int y, x, y2, x2;
}p4;
int main()
{
	int h, w;
	ll M = 0;
	vector<p4> k;
	string ans = "";
	cin >> h >> w;
	vector<vector<int>> c(h, vector<int>(w));
	rep(i, 0, h) {
		rep(i2, 0, w) {
			cin >> c[i][i2];
		}
	}
	int x, y, px = -1, py;
	rep(i, 0, h) {
		rep(i2, 0, w) {
			y = i, x = i % 2 ? (w - i2 - 1) : i2;
			if (px != -1 && c[py][px] % 2) {
				c[py][px]--;
				c[y][x]++;
				k.push_back({py+1,px+1, y+1, x+1});
				M++;
			}
			px = x, py = y;
		}
	}
	printf("%lld\n", M);
	for (auto it : k) {
		printf("%d %d %d %d\n", it.y, it.x, it.y2, it.x2);
	}
	return 0;
}


#include <iostream>
using namespace std;
int prime_number(int n){
    int i;
    if(n<=1) return 0;
    for(i=2;i<=n/2;i++) if(n%i==0) return 0;
    return 1;
}

int main(){
    int i,j,k,n[10000],a=0;
    for(j=0;j<10000;j++){
        cin >> n[j];
        if(n[j]==0) break;
    }
    for(k=0;k<j;k++){
        for(i=n[k];i>4;i--) if(prime_number(i)) if(prime_number(i-2)){
            a=i;
            break;
        }
        if(a!=0) cout << i-2 << " " << i << endl;
    }
    return 0;
}

#include <cstdio>
#include <cstring>
#include <cmath>
#include <utility>
#include <iostream>
#include <functional>
#include <bitset>
#include <algorithm>
#include <vector>
#include <forward_list>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <numeric>
#include <iomanip>
#define ll long long int
#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
using namespace std;
int mx4[] = {0,1,0,-1};
int my4[] = {1,0,-1,0};
int mx2[] = {1,0};
int my2[] = {0,1};

class CXYetAnotherDieGame {
public:
	void solve(istream& in, ostream& out) {
        ll x;
        in >> x;
        if(x <= 6) out << 1 << endl;
        else if(x % 11 > 6) out << (x / 11) * 2 + 2 << endl;
        else if(x % 11 <= 6 && x % 11 != 0) out << (x / 11) * 2 + 1 << endl;
        else if(x % 11 == 0) out << (x / 11) * 2 << endl;
	}
};

int main() {
	CXYetAnotherDieGame solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) for (int i = (int)(n-1); i >= 0; i--)
#define sz(x) int(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define INF 2e9
#define LINF 1e18
#define mod 1e9+7
template<class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> P;

int main() {
    int n; cin >> n;
    vector<int> p(n);
    rep(i,n) cin >> p[i];
    int mini = n+1;
    int ans = 0;
    rep(i,n) {
        if (p[i] <= mini) {
            mini = p[i];
            ans++;
        }
    }
    cout << ans << endl;
}

////////////////////////// Author
////////////////////////// Nasim Hossain Rabbi
////////////////////////// JU-CSE28
////////////////////////// CF - imnasim3.1415
////////////////////////// UVA - imnasim3.1415
////////////////////////// Mail - imnasim3.1415@gmail.com
#include<bits/stdc++.h>
using namespace std;
#define E end()
#define B begin()
#define sz size()
#define EM empty()
#define fi first
#define se second
#define cl clear()
#define sqr(a) (a*a)
#define mk make_pair
#define po pop()
#define pu push
#define pb push_back
#define pf push_front
#define pob pop_back()
#define pof pop_front()
string en="\n";
string sp=" ";
string t="hello";
string Y="YES\n";
string N="NO\n";
#define rep(i,n) for(i=0;i<n;i++)
#define Rep(i,n) for(i=1;i<=n;i++)
#define per(i,n) for(i=n-1;i>=0;i--)
#define peR(i,n) for(i=n;i>0;i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define all(cont) cont.begin(),cont.end()
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL)
#define pi 3.1415926535897932384626433832795
#define vi vector<long long>
#define vs  vector<string>
#define MX LLONG_MAX
#define MN LLONG_MIN
#define MOD 1000000007
#define vp(vi,x) cin>>x; vi.pb(x);
#define bsearch(a,x) binary_search(all(a),x)
#define LB(a,x) (lower_bound(all(a),x)-a.B)
#define UB(a,x) (upper_bound(all(a),x)-a.B)
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
bool isprm(LL a){if(a==1)return false; for(LL i=2;i*i<=a;i++){if(a%i==0)return false;}return true;}
bool palin(string a){for(int i=0;i<a.sz;i++){if(a.at(i)!=a.at(a.sz-i-1))return 0;} return 1;}
bool isVowel(char ch){ ch=toupper(ch); if(ch=='A'||ch=='U'||ch=='I'||ch=='O'||ch=='E') return true; return false;}
int toInt(string s){int sm;stringstream ss(s);ss>>sm;return sm;}
LL ceil2(LL a,LL b){LL c=a/b; if(a%b)c++; return c; }
LL bpow(LL a,LL b){if(b==0)return 1;LL r=bpow(a,b/2);if(b%2)return r*r*a;else return r*r;}
LL binpow(LL a,LL b){LL r=1;while(b>0){if(b&1)r=r*a;a=a*a;b>>=1;}return r;}
LL binpow(LL a,LL b,LL m){a%=m;LL r=1;while(b>0){if(b&1) r=r*a%m;a=a*a%m; b>>=1;}return r;}
template<typename T>inline T gcd(T a,T b){if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}
template<typename T>inline T lcm(T a,T b) {if(a<0)return lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));}
int main()
{
//    fast;
    LL i,j,k,n,m,l,s=0,x,y,tc=1,p,q,a,b,c=0;
    bool f=0,ff=0;string st,s1,s2,s3; char ch;
    while(cin>>n)
    {
        c=0,a=MX;
        rep(i,n)
        {
            cin>>x;
            if(x<a)c++,a=x;
        }
        cout<<c<<en;
    }
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
using namespace __gnu_pbds;
using namespace std;
/*
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
*/
#define ull unsigned long long int
#define ll long long int
#define MAX 1000000007
#define mod 100003
//1e9+7
#define pll pair<ll,ll>
#define pii pair<int,int>
#define piii pair<int,pii>
#define IO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define FO ifstream fin("a.in");ofstream fout("a.out");
#define TEST int testcase;cin>>testcase;while(testcase--)
#define all(x) (x).begin(),(x).end()
#define mset0(x) memset((x), 0, sizeof((x)));
#define mset1(x) memset((x), -1, sizeof((x)));
#define endl "\n"
#define plll pair<ll,pll>
#define piiii pair<int,piii>
typedef tree<ll,null_type,less<ll>,rb_tree_tag, tree_order_statistics_node_update> indexed_set;
FO
#define no {cout<<"NO"<<endl;return;}
#define yes {cout<<"YES"<<endl;return;}

void solve(){
    int n,k;
    cin>>n>>k;
    long double maxx=0,sum=0;
    long double p[n+1];
    for(int i=1;i<=n;i++) cin>>p[i];
    for(int i=1;i<=k;i++){
        sum+=0.5*(p[i]+1);
    }
    maxx=max(maxx,sum);
    for(int j=k+1;j<=n;j++){
        //cout<<i<<" "<<j<<endl;
        sum-=0.5*(p[j-k]+1);
        sum+=0.5*(p[j]+1);
        maxx=max(maxx,sum);
    }
    cout<<setprecision(12)<<maxx;

    return;
}
int main(){
    IO
    //TEST
    solve();
    return(0);
}

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
int main(){
    ios::sync_with_stdio(false);
    int n,k;
    cin >> n >> k;
    vector<int> p(n);
    rep(i,n) {
        cin >> p[i];
    }
    vector<int> s(n-k+1,0);
    rep(i,k) s[0] += p[i];
    rep(i,n-k) s[i+1] = s[i] - p[i] + p[i+k];
    int ans = 0;
    rep(i,n-k+1) chmax(ans, s[i]);
    cout << std::fixed << (ans+k)/2.0  << endl;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;

#ifdef LOCAL
	#define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr);
#else
	#define eprintf(...) 42
#endif

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template<typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
	return (ull)rng() % B;
}

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second

clock_t startTime;
double getCurrentTime() {
	return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}

const ll MOD = (ll)1e9 + 7;
ll add(ll x, ll y) {
	x += y;
	if (x >= MOD) return x - MOD;
	return x;
}
ll sub(ll x, ll y) {
	x -= y;
	if (x < 0) return x + MOD;
	return x;
}
ll mult(ll x, ll y) {
	return (x * y) % MOD;
}
ll bin_pow(ll x, ll p) {
	if (p == 0) return 1;
	if (p & 1) return mult(x, bin_pow(x, p - 1));
	return bin_pow(mult(x, x), p / 2);
}
ll rev(ll x) {
	return bin_pow(x, MOD - 2);
}

const int N = (1 << 16) + 7;
int dp[2][N];
int n, m, W;

void read() {
	int x, y, z;
	scanf("%d%d%d%d", &n, &x, &y, &z);
	m = x + y + z - 1;
	W = (1 << (x + y + z - 1)) ^ (1 << (y + z - 1)) ^ (1 << (z - 1));
}

int main()
{
	startTime = clock();
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	read();
	dp[0][0] = 1;
	while(n--) {
		for (int i = 0; i <= (1 << m); i++)
			dp[1][i] = 0;
		dp[1][1 << m] = add(dp[1][1 << m], mult(dp[0][1 << m], 10));
		for (int mask = 0; mask < (1 << m); mask++) {
			for (int x = 1; x <= 10; x++) {
				int nmask = (mask << x) ^ (1 << (x - 1));
				if ((nmask & W) == W)
					nmask = 1 << m;
				else
					nmask &= (1 << m) - 1;
				dp[1][nmask] = add(dp[1][nmask], dp[0][mask]);
			}
		}
		for (int i = 0; i <= (1 << m); i++)
			dp[0][i] = dp[1][i];
	}
	printf("%d\n", dp[0][1 << m]);

	return 0;
}

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int main()
{
	vi w(10),k(10);
	rep(i,10)
		cin>>w[i];
	rep(i,10)
		cin>>k[i];
	sort(allof(w),greater<int>());
	sort(allof(k),greater<int>());
	cout<<accumulate(allof(w)-7,0)<<" "<<accumulate(allof(k)-7,0)<<endl;

	return 0;
}


#include<iostream>
#include<cmath>

using namespace std;

int main() {
  int N, Y;
  cin >> N >> Y;
  string ans = "-1 -1 -1";
  for (int i = 0; i <= N; ++i) {
    for (int j = 0; j <= N; ++j) {
      int k = N - j - i;
      if (k < 0) continue;
      if (i * 10000 + j * 5000 + k * 1000 == Y) {
        ans = to_string(i) + ' ' + to_string(j) + ' ' + to_string(k);
        break;
      }
    }
    if (ans != "-1 -1 -1") break;
  }
  cout << ans << endl;
}

#include <bits/stdc++.h>

using namespace std;

int main(){

    int a,b;
    cin>>a>>b;
    string x,y;
    int sum = 0;
    for(;a<=b;a++){
    	y = x = to_string(a);
    	reverse(y.begin(),y.end());
    	if(x==y) sum++;
    }

    cout<<sum;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 5, LOG = 30;
int A[N], B[N], cnt[N], f[N], w[N], n;
bool a[N], b[N];

void Check() {
    int curr = 0;
    for (int i = 2; i <= n; i ++) {
        while (curr && b[curr] != b[i - 1]) curr = f[curr];
        if (b[curr] == b[i - 1]) curr++;
        f[i] = curr;
    }
    curr = 0;
    for (int i = 1; i <= n; i++) {
        while (curr && b[curr] != a[i - 1]) curr = f[curr];
        if (b[curr] == a[i - 1]) curr++;
    }
    memset(w, 0, sizeof w);
    while (curr) w[curr] = 1, curr = f[curr];
    w[0] = 1;
    for (int i = 2; i <= n; i ++) {
        while (curr && a[curr] != a[i - 1]) curr = f[curr];
        if (a[curr] == a[i - 1]) curr++;
        f[i] = curr;
    }
    curr = 0;
    for (int i = 1; i <= n; i++) {
        while (curr && a[curr] != b[i - 1]) curr = f[curr];
        if (a[curr] == b[i - 1]) curr++;
    }
    while (curr) {
        if (w[n - curr] == 1) cnt[curr]++;
        curr = f[curr];
    }
    if (w[n] == 1) cnt[0]++;
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++) cin >> A[i];
    for (int i = 0; i < n; i++) cin >> B[i];
    for (int j = 0; j < LOG; j++) {
        for (int i = 0; i < n; i++) a[i] = (A[i]>>j)&1, b[i] = (B[i]>>j)&1;
        Check();
        for (int i = 0; i < n; i++) a[i] ^= 1;
        Check();
    }
    for (int i = 0; i < n; i++) if (cnt[i] == LOG) cout << i << " " << (A[i]^B[0]) << "\n";
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define ALL(a)  (a).begin(),(a).end()
constexpr int B = 100000007;
constexpr int MOD = 1000000007;
constexpr int INF = 2147483647;
void yes(bool expr) {
  cout << (expr ? "Yes" : "No") << "\n";
}
lint power(lint x, lint n, lint mod) {
  lint ret = 1;
  while(n > 0) {
    if(n & 1) (ret *= x) %= mod;
    (x *= x) %= mod;
    n >>= 1;
  }
  return ret;
}
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int N;
  cin >> N;
  vector<lint> a(N);
  REP(i, N) cin >> a[i];
  vector<lint> b(N);
  REP(i, N) cin >> b[i];
  vector<lint> pa(N);
  vector<lint> pb(N);
  REP(i, N) {
    pa[i] = a[i] ^ a[(i+1)%N];
    pb[i] = b[i] ^ b[(i+1)%N];
  }
  lint hash = 0;
  lint target = 0;
  REP(i, N) {
    hash = (hash + pa[i]*power(B, i, MOD)) % MOD;
    target = (target + pb[i]*power(B, i, MOD)) % MOD;
  }
  vector<pair<lint, lint>> ans;
  REP(i, N) {
    //cout << hash << " " << target << endl;
    if(hash == target) {
      lint x = b[0] ^ a[i];
      ans.push_back(make_pair(i, x));
    }
    hash = hash * power(B, MOD-2, MOD) % MOD;
    hash = (hash - pa[i]*power(B, MOD-2, MOD) % MOD + pa[i]*power(B, N-1, MOD) + MOD) % MOD;
  }
  sort(ans.begin(), ans.end(), [](auto const &a, auto const &b) { return a.first < b.first; });
  REP(i, ans.size()) {
    cout << ans[i].first << " " << ans[i].second << "\n";
  }

}


#include <cstdio>
#include <algorithm>
#include <vector>
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)

inline int read()
{
	int data = 0, w = 1; char ch = getchar();
	while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
	if (ch == '-') w = -1, ch = getchar();
	while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();
	return data * w;
}

const int N(2e5 + 10), Mod(1e9 + 7);
int fac[N], inv[N], f[N], n, m, A, B;
int C(int n, int m) { return 1ll * fac[n + m] * inv[m] % Mod * inv[n] % Mod; }
int fastpow(int x, int y)
{
	int ans = 1;
	for (; y; y >>= 1, x = 1ll * x * x % Mod)
		if (y & 1) ans = 1ll * ans * x % Mod;
	return ans;
}

int main()
{
	n = read() - 1, m = read() - 1, A = read() - 1, B = read() - 1, fac[0] = inv[0] = 1;
	if (A == n || B == m) return puts("0"), 0;
	for (int i = 1; i <= n + m; i++) fac[i] = 1ll * fac[i - 1] * i % Mod;
	inv[n + m] = fastpow(fac[n + m], Mod - 2);
	for (int i = n + m - 1; i; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % Mod;
	int ans = C(n, m);
	for (int i = 0; i <= A; i++)
		ans = (ans - 1ll * C(i, m - B - 1) * C(n - i, B) % Mod + Mod) % Mod;
	printf("%d\n", ans);
	return 0;
}

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int wh, bk; cin >> wh >> bk;
    int h = 100 / 2, w = 100;
    cout << h * 2 << " " << w << endl;
    auto func = [&](char c0, char c1, int cnt) {
        vector<string> s(h, string(w, c0));
        for (int i = 1; i < h - 1; i += 2) for (int j = 0; j < w; j += 2) {
            if (cnt) s[i][j] = c1, cnt--;
        }
        for (auto &si: s) cout << si << endl;
    };
    func('.', '#', bk - 1);
    func('#', '.', wh - 1);
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pp pair<pair<ll, ll>,pair<ll, ll>>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define vii vector<int>
#define vll vector<ll>
#define mat vector<vector<ll>>
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define eb emplace_back
#define fi first
#define sc second
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define all(x) x.begin(),x.end()
#define sz(x) (ll) (x).size()
#define pq priority_queue<ll>
#define pqg priority_queue<ll,vector<ll>,greater<ll>>
#define LB(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin())
#define UB(v,x) (upper_bound(v.begin(),v.end(),x)-v.begin())
#define ERASE(v) sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end())
#define int ll
// #define ll int
using namespace std;
const ll INF = (1 << 30 ) - 1;
const ll LLINF = (1LL << 60LL);
const ll MOD = 1000000007;
const ll mod = 998244353;
const ll MAX = 2100000;
const double pi = acos(-1);
const double eps = 1e-10;
ll dx[8] ={1,0,-1,0,1,-1,1,-1};
ll dy[8] ={0,1,0,-1,1,-1,-1,1};

template<class T>
inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>
inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

void solve(){
    ll n;
    ll x[200010],y[200010];
    char u[200010];
    set<ll> v[200010],w[200010];
    ll ans=LLINF;
    cin>>n;
    rep(i,n){
        cin>>x[i]>>y[i]>>u[i];
    }
    rep(i,n){
        if(u[i]=='D') v[x[i]].insert(y[i]);
        else if(u[i]=='L') w[y[i]].insert(x[i]);
    }
    rep(i,n){
        if(u[i]=='U'){
            if(v[x[i]].lower_bound(y[i])==v[x[i]].end()) continue;
            ll z=*v[x[i]].lower_bound(y[i]);
            chmin(ans,(z-y[i])*5);
        }
        else if(u[i]=='R'){
            if(w[y[i]].lower_bound(x[i])==w[y[i]].end()) continue;
            ll z=*w[y[i]].lower_bound(x[i]);
            chmin(ans,(z-x[i])*5);
        }
    }

    set<pll> a[400010],b[400010];
    set<pll> c[400010],d[400010];
    rep(i,n){
        ll X=x[i]+y[i],Y=x[i]-y[i]+200000;
        if(u[i]=='U'){
            a[X].insert({x[i],y[i]});

        }
        else if(u[i]=='D'){
            c[Y].insert({x[i],y[i]});

        }
        else if(u[i]=='L'){
            b[X].insert({x[i],y[i]});
            d[Y].insert({x[i],y[i]});

        }
    }
    rep(i,n){
        ll X=x[i]+y[i],Y=x[i]-y[i]+200000;
        if(u[i]=='R'){
            if(a[X].lower_bound({x[i],y[i]})==a[X].end()){}
            else {
                pll z=*a[X].lower_bound({x[i],y[i]});
                chmin(ans,(y[i]-z.sc+z.fi-x[i])*5);
            }

            if(c[Y].lower_bound({x[i],y[i]})==c[Y].end()){}
            else {
                pll z=*c[Y].lower_bound({x[i],y[i]});
                chmin(ans,(z.sc-y[i]+z.fi-x[i])*5);
            }
        }
        else if(u[i]=='U'){
            if(d[Y].lower_bound({x[i],y[i]})==d[Y].end()){}
            else{
                pll z=*d[Y].lower_bound({x[i],y[i]});
                chmin(ans,(z.fi-x[i]+z.sc-y[i])*5);
            }
        }
        else if(u[i]=='D'){
            if(b[X].lower_bound({x[i],y[i]})==b[X].end()){}
            else {
                pll z=*b[X].lower_bound({x[i],y[i]});
                chmin(ans,(z.fi-x[i]+y[i]-z.sc)*5);
            }
        }

    }

    if(ans==LLINF) cout<<"SAFE"<<endl;
    else cout<<ans<<endl;

}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);

    solve();

    return 0;
}

// This code wrote by chtholly_micromaker(MicroMaker)
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define reg register
using namespace std;
const int MaxN=2050;
const int dir[4][2]={1,0,0,1,-1,0,0,-1};
template <class t> inline void read(t &s)
{
	s=0;
	reg int f=1;
	reg char c=getchar();
	while(!isdigit(c))
	{
		if(c=='-')
			f=-1;
		c=getchar();
	}
	while(isdigit(c))
		s=(s<<3)+(s<<1)+(c^48),c=getchar();
	s*=f;
	return;
}
char G[MaxN][MaxN];
int f[MaxN][MaxN],g[MaxN][MaxN],h[MaxN][MaxN];
int n,m;
signed main(void)
{
	// freopen("big.in","r",stdin);
	int q;
	cin>>n>>m>>q;
	for(int i=1;i<=n;++i)
		scanf("%s",G[i]+1);
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j)
		{
			f[i][j]=f[i-1][j]+f[i][j-1]-f[i-1][j-1]+(G[i][j]=='1'?1:0);
			g[i][j]=g[i-1][j]+g[i][j-1]-g[i-1][j-1];
			h[i][j]=h[i-1][j]+h[i][j-1]-h[i-1][j-1];
			if(G[i][j]=='1')
			{
				if(G[i-1][j]=='1')
					++g[i][j];
				if(G[i][j-1]=='1')
					++h[i][j];
			}
		}
	reg int U,V,X,Y;
	for(int qq=0;qq<q;++qq)
	{
		read(U);read(V);read(X);read(Y);
		reg int ans=f[X][Y]-f[U-1][Y]-f[X][V-1]+f[U-1][V-1];
		// printf("t1 %d\n",f[X][Y]-f[U-1][Y]-f[X][V-1]+f[U-1][V-1]);
		ans-=g[X][Y]-g[U][Y]-g[X][V-1]+g[U][V-1];
		// printf("t2 %d\n",g[X][Y]-g[U][Y]-g[X][V-1]+g[U][V-1]);
		ans-=h[X][Y]-h[U-1][Y]-h[X][V]+h[U-1][V];
		// printf("t3 %d\n",h[X][Y]-h[U-1][Y]-h[X][V]+h[U-1][V]);
		printf("%d\n",ans);
	}
	return 0;
}


#include<iostream>
using namespace std;
int a;
int main() {
	while (cin >> a) {
		cout << (900 - a)*(900 - a) * 100 - 9000000 << endl;
	}
}

#include <fstream>
#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
using namespace std;

int f(int x) {
  return x * x;
}

int main() {
  int d;
  int D = 600;
  while (cin >> d) {
    int s = 0;
    for (int i = 1; i * d < D; i++)
      s += f(i * d) * d;

    cout << s << endl;
  }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for(int i = a; i < b; i++)
int a, b, c, d, e, k;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> a >> b >> c >> d >> e >> k;
    bool flg = true;
    if(e - a > k) {
        flg = false;
    }

    if(flg) {
        cout << "Yay!" << endl;
    } else {
        cout << ":(" << endl;
    }
    return 0;
}

#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<stack>
#include<queue>
#include<tuple>
#include<cassert>
#include<set>
#include<functional>
//#include<bits/stdc++.h>
using ll = long long;
using namespace std;
const ll INF = 1000000000000000000;
const ll mod = 1000000007;
//printf("%.10f\n", n);
ll yo[12345678], ta[12345678];
signed main() {
    ll n = 0, m;
    string s; cin >> s;
    if (s.size() % 2 == 1) { cout << "No" << endl; return 0; }
    for (int h = 0; h < s.size(); h++) {
        if (h % 2 == 0) {
            if (s[h] != 'h') { n++; }
        }
        else {
            if (s[h] != 'i') { n++; }
        }
    }
    if (n == 0) { cout << "Yes" << endl; }
    else { cout << "No" << endl; }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <iomanip>
#include <set>
#include <ctime>
#include <tuple>
#include <bitset>
#include <assert.h>
#include <deque>
using namespace std;
typedef long long ll;

#define fi first
#define se second
#define rep(i, n) for (ll i = 0; i < n; i++)
#define debugA cerr << "AAAAA" << endl
#define debug_ cerr << "-------------" << endl
#define debug(x) cerr << #x << ": " << x << endl
#define debug_vec(v)                   \
    cout << #v << endl;                \
    for (int i = 0; i < v.size(); i++) \
    {                                  \
        cout << v[i] << " ";           \
    }                                  \
    cout << endl;
#define debug_vec2(v)                         \
    cout << #v << endl;                       \
    for (int i = 0; i < v.size(); i++)        \
    {                                         \
        for (int j = 0; j < v[i].size(); j++) \
        {                                     \
            cout << v[i][j] << " ";           \
        }                                     \
        cout << endl;                         \
    }

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

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

using Graph = vector<vector<int>>;
using P = pair<int, int>;
using P1 = pair<int, pair<int, int>>; // クラスカル法とかで、{cost, {from, to}}的に使う。
const ll INF = 1001001001001001001;
const double pi = acos(-1);

int main()
{
    int n, k;
    cin >> n >> k;
    vector<int> v(n);
    for (int i = 0; i < n; i++)
    {
        cin >> v[i];
    }
    //左からi個取り出す
    //右からj個取り出す
    ll ans = 0;
    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j <= n; j++)
        {
            if (i + j > k || i + j > n)
            {
                break;
            }
            ll tmp = 0;
            priority_queue<int, vector<int>, greater<int>> que;
            for (int l = 0; l < i; l++)
            {
                tmp += v[l];
                que.push(v[l]);
            }
            for (int l = 0; l < j; l++)
            {
                tmp += v[n - 1 - l];
                que.push(v[n - 1 - l]);
            }
            for (int l = 0; l < k - i - j; l++)
            {
                if (que.empty())
                {
                    break;
                }
                int x = que.top();
                if (x >= 0)
                {
                    break;
                }
                que.pop();
                tmp -= x;
            }
            ans = max(ans, tmp);
        }
    }
    cout << ans << endl;

    return 0;
}

#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD 1000000007
#define MOD2 998244353
#define MOD3 1000000009
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
//ifstream cinn("in.in");ofstream coutt("out.out");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}
int ceil(const int &a,const int &b){return (a+b-1)/b;}
////Read:
//Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(),

int n,c,dp[55][55][55][55];
vi v;

int f(int i,int cnt,int mxgap,int lst){
    if(i==n){
        mxgap=max(mxgap,i-lst-1);
        if(cnt+(n-cnt-mxgap)*2<=c)return 0;
        return -INF;
    }
    int &ans=dp[i][cnt][mxgap][lst+1];
    if(ans!=-INF)return ans;
    ans=v[i]+f(i+1,cnt+1,max(mxgap,i-lst-1),i);
    ans=max(ans,f(i+1,cnt,mxgap,lst));
    return ans;
}

signed main(){
    FASTER;
    cin>>n>>c;
    v=vi(n);cin>>v;
    for(int i=0;i<55;i++){
        for(int j=0;j<55;j++){
            for(int k=0;k<55;k++){
                for(int l=0;l<55;l++){
                    dp[i][j][k][l]=-INF;
                }
            }
        }
    }
    cout<<f(0,0,0,-1);
}


#include <iostream>
#include <regex>
using namespace std;

int main() {
	string S;
	cin >> S;
	cout << (regex_search(S.begin(), S.end(), regex("AC")) ? "Yes" : "No") << endl;
}

#include <bits/stdc++.h>		//万能头文件
using namespace std;

int main()
{
	string a;
	int pos=0;		//记录位置
	cin>>a;
	a=a+"D";		//为防止最后一个字母是"A",添加一个不是"C"的字母
	while(1)
	{
		pos=a.find("A",pos);		//find()函数查找位置
		if(pos<0)		//如果没找到，返回-1
		{
			cout<<"No"<<endl;		//endl养成好习惯
			break;		//跳出循环
		}
		if(a[pos+1]=='C')		//判断"A"下一个是不是"C"
		{
			cout<<"Yes"<<endl;
			break;		//跳出循环
		}
		pos++;		//下一次从pos+1找起
	}
	return 0;
}


#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <math.h> // sqrt
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <iomanip> // setprecision
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vb = vector<bool>;
using vll = vector<long long>;
using pii = pair<int, int>;
using psi = pair<string, int>;

int N;

int f(vector<vector<pair<int, int>>> &data, vi &bit, int n) {
    if (n == 0) {
        int ans = 0;
        bool check = false;
        for (int i = 0; i < N; i++) {
            if (bit[i] == 0) {
                continue;
            }
            ans++;
            for (pair<int, int> p : data[i]) {
                if (bit[p.first - 1] != p.second) {
                    return 0;
                }
            }
        }
        return ans;
    }
    bit[N - n] = 1;
    int a = f(data, bit, n - 1);
    bit[N - n] = 0;
    int b = f(data, bit, n - 1);
    return max(a, b);
}

int main() {
    cin >> N;
    vi a(N);
    vector<vector<pair<int, int>>> data(N);
    for (int i = 0; i < N; i++) {
        cin >> a[i];
        vector<pair<int, int>> v(a[i]);
        for (int j = 0; j < a[i]; j++) {
            int x, y;
            cin >> x >> y;
            v[j] = {x, y};
        }
        data[i] = v;
    }
    vi bit(N, 0);
    int ans = f(data, bit, N);
    cout << ans << endl;
}

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }

#define N 8
bool row[N], col[N], dpos[2 * N - 1], dneg[2 * N - 1];
bool X[N][N];

void print_board() {
	rep(i, 0, N) {
		rep(j, 0, N) {
			if (X[i][j])cout << 'Q';
			else cout << '.';
		}
		cout << endl;
	}
}

void dfs(int i) {
	if (i == N) {
		print_board();
		return;
	}
	rep(j, 0, N) {
		if (X[i][j]) {
			dfs(i + 1);
			return;
		}
	}

	rep(j, 0, N) {
		if (row[i] || col[j] || dpos[i + j] || dneg[i - j + N - 1])continue;
		X[i][j] = true;
		row[i] = col[j] = dpos[i + j] = dneg[i - j + N - 1] = true;
		dfs(i + 1);
		X[i][j] = false;
		row[i] = col[j] = dpos[i + j] = dneg[i - j + N - 1] = false;
	}
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n; cin >> n;
	rep(i, 0, n) {
		int y, x; cin >> y >> x;
		X[y][x] = true;
		row[y] = col[x] = dpos[y + x] = dneg[y - x + N - 1] = true;
	}
	dfs(0);
	return 0;
}


#include <iostream>
#include <vector>
#include <utility>
using namespace  std;

void fnInput(int &rnMaxSiz, vector<int> &rvnNum)
{
  cin >> rnMaxSiz;

  rvnNum.resize(rnMaxSiz);
  for (int i = 0; i < rnMaxSiz; i++)
    cin >> rvnNum[i];
}

void fnPartition(vector<int> &rvnNum, int nStart, int nEnd, int &nCur)
{
  for (int i = nStart; i < nEnd; i++)
    if (rvnNum[i] <= rvnNum[nEnd])
      swap(rvnNum[nCur++], rvnNum[i]);

  swap(rvnNum[nCur], rvnNum[nEnd]);
}

void fnResult(const vector<int> &cnrvnNum, int nCur)
{
  for (int i = 0; i < cnrvnNum.size(); i++)
  {
    if (i)  cout << " ";
    if (i == nCur) cout << "[";
    cout << cnrvnNum[i];
    if (i == nCur) cout << "]";
  }
  cout << endl;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  int nMaxSiz;
  vector<int> vnNum;

  fnInput(nMaxSiz, vnNum);
  int nStart = 0;
  int nEnd = vnNum.size() - 1;
  int nCur = nStart;

  fnPartition(vnNum, nStart, nEnd, nCur);
  fnResult(vnNum, nCur);

  return 0;
}

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
int partition(vector<int>&a, int l, int r) {
	int v = a[r];
	int i(l - 1);
	rep(j, l, r) {
		if (a[j] <= v) {
			i++;
			swap(a[i], a[j]);
		}
	}
	swap(a[i + 1], a[r]);
	return i + 1;
}
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N; cin >> N;
	vector<int> v(N); rep(i, 0, N) { cin >> v[i]; }
	int x = partition(v, 0, N - 1);
	rep(i, 0, N) {
		if (i == x)cout << '[' << v[i] << ']';
		else cout << v[i];
		cout << (i == N - 1 ? '\n' : ' ');
	}
	return 0;
}


#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(){

	int N,Q,M,Date[101],i,l,k;

	while(0<=scanf("%d%d",&N,&Q)){
		for(i=0;i<101;i++){
			Date[i]=0;
		}

		if((N==0) && (Q==0)){
			break;
		}

		for(k=0;k<N;k++){
			scanf("%d",&M);
			if(M==0){
				continue;
			}else{
				for(i=0;i<M;i++){
					scanf("%d",&l);
					++Date[l];
				}
			}
		}
		l=0;
		i=0;
		for(k=0;k<100;k++){
			if(i<Date[k] && Date[k]>=Q){
				i=Date[k];
				l=k;
			}
		}

		printf("%d\n",l);
	}

	return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define irep(i, n) for (int i = (n); i >= 0; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1 << 25;
const int MOD = 1e9+7;
char m[50][50];
int dist[100][100];
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};

int main() {
	int h,w;
	cin >> h >> w;
	int numOfW = 0;
	rep(i,h) rep(j,w) {
		cin >> m[i][j];
		if (m[i][j] == '.') numOfW++;
	}

	rep(i,h) rep(j,w) dist[i][j] = -1;
	queue<P> q;
	q.push({0,0});
	dist[0][0] = 1;

	while (!q.empty())
	{
		P p = q.front(); q.pop();
		int x = p.first, y = p.second;
		rep(i,4) {
			int nx = x+dx[i], ny = y+dy[i];
			if (nx<0||nx>=h||nx<0||ny>=w) continue;
			if (m[nx][ny]=='#'||dist[nx][ny]!=-1) continue;
			dist[nx][ny] = dist[x][y] + 1;
			q.push({nx,ny});
		}
	}

	if (dist[h-1][w-1] == -1) cout << -1 << endl;
	else cout << numOfW - dist[h-1][w-1] << endl;

	return 0;
}