#include <bits/stdc++.h>
using namespace std;
using P = pair<int, long long>;

int main() {
  long long n,m;
  long long a,b,c,d;
  cin >> n >> m;
  vector<vector<long long>> data;
  vector<vector<long>> path(n,vector<long>());
  for(int i=0;i<m;i++) {
    cin >> a >> b >> c >> d;
    a--; b--;
    data.push_back({a,b,c,d,(long long)(sqrt(d*1.0)+0.5)-1});
    path[a].push_back({i});
    path[b].push_back({i});
  }
  bitset<101010> flag;
  stack<int> st;
  st.push(0);
  int now, next;
  while(!st.empty()){
    now = st.top();
    st.pop();
    flag[now]=true;
    for(int i=0;i<path[now].size();i++){
      if(data[path[now][i]][0]==now) next = data[path[now][i]][1];
      else next = data[path[now][i]][0];
      if(!flag[next])flag[next]=1, st.push(next);
    }
  }
  if(!flag[n-1]) {
    cout << "-1" << endl;
    return 0;
  }
  vector<long long> dis(n, 1000000000000000LL);
  dis[0]=0;
  auto comp = [](P l, P r) { return l.second>r.second; };
  priority_queue<P, vector<P>, decltype(comp)> q(comp);
  q.push({0,0});
  pair<int,long long> u;
  long long tmin;
  int to;
  while(!q.empty()){
    u=q.top();
    q.pop();
    now=u.first;
    if(dis[now]<u.second) continue;
    for(int i=0;i<path[now].size();i++){
      to=path[now][i];
      tmin=data[to][2];
      if(u.second<=data[to][4]){
        tmin+=data[to][4]+data[to][3]/(data[to][4]+1);
      } else {
        tmin+=u.second+data[to][3]/(u.second+1);
      }
      if(data[path[now][i]][0]==now) next = data[path[now][i]][1];
      else next = data[path[now][i]][0];
      if(tmin<dis[next]){
        dis[next]=tmin;
        q.push({next,tmin});
      }
    }
  }
  cout << dis[n-1] << endl;
}

//https://atcoder.jp/contests/abc188/tasks/abc188_d
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;

int main ()
{
    ios_base::sync_with_stdio (0); cin.tie (0); cout.tie (0);
    ll n, c, ans = 0;
    cin >> n >> c;
    vector<pair<ll, ll>> v;
    for (ll i = 0; i < n; ++i)
    {
        ll a, b, c;
        cin >> a >> b >> c;
        v.push_back ({a - 1, c});
        v.push_back ({b, -c});
    }
    sort (v.begin (), v.end ());
    ll t = 0, p = 0;
    for (auto& x : v)
    {
        if (x.F != t)
        {
            ans += min (c, p) * (x.F - t);
            t = x.F;
        }
        p += x.S;
    }
    cout << ans;
}


#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define m_p make_pair
#define re return
#define pb push_back
#define si set<int>
#define ld long double
#define X first
#define Y second
#define st string
#define ull unsigned long long
#define mod 1000000007
#define INF 1000000007
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++)
using namespace std;
inline void read(int &x)
{
	short negative=1;
    x=0;
    char c=getchar();
    while(c<'0' || c>'9')
    {
		if(c=='-')
			negative=-1;
		c=getchar();
	}
    while(c>='0' && c<='9')
        x=(x<<3)+(x<<1)+(c^48),c=getchar();
    x*=negative;
}
ll quickpower(ll n,ll k){
	ll ans=1;
	while(k){
		if(k%2){
			ans*=n;
			ans%=mod;
		}
		n*=n;
		n%=mod;
		k/=2;
	}
	return ans;
}
string int_to_string(int n)
{
	string s="";
	while(n)
	{
		int now=n%10;
		s+=now+'0';
		n/=10;
	}
	reverse(s.begin(),s.end());
	return s;
}
int string_to_int(string s)
{
	int n=0;
	rep(i,s.size())
	{
		n*=10;
		n+=s[i]-'0';
	}
	return n;
}
int n,m;
int a[111][111];
int main()
{
	ios::sync_with_stdio(0);
	cin>>n>>m;
	rep(i,n)rep(j,m)cin>>a[i][j];
	int mn=1e9;
	rep(i,n)rep(j,m)mn=min(mn,a[i][j]);
	ll ans=0;
	rep(i,n)rep(j,m)ans+=a[i][j]-mn;
	cout<<ans<<endl;
	return 0;
}

#include <bits/stdc++.h>

const int N = 200000;

int v[2*N + 5], n;

int main() {
	scanf("%d", &n); for(int i=1;i<=(n<<1);i++) scanf("%d", &v[i]);

	long long sum = 0;
	std::priority_queue<int>que;
	for(int i=n;i>=1;i--) {
		que.push(v[n - i + 1]), que.push(v[n + i]);
		sum += que.top(), que.pop();
	}
	printf("%lld\n", sum);
}


///17571@nith.ac.in @Ankit Verma
#include <bits/stdc++.h>
using namespace std;
#define ff          first
#define ss          second
#define pb          push_back
#define MP          make_pair
#define int         long long
#define ll          long long
#define inf         0x3f3f3f3f
#define MOD         1000000007
#define ld          long double
#define eb          emplace_back
#define pii         pair<int,int>
#define ull         unsigned long long
#define all(v)      v.begin(), v.end()
#define f(i,x,n)    for(int i=x;i<n;i++)
#define fr(i,x,n)   for(int i=x;i>=n;i--)
#define fastio      ios::sync_with_stdio(false)

#define n_l '\n'
template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)),  __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); }
#define debug(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl

void IO() {
    #ifndef ONLINE_JUDGE
        freopen("/home/ankit/Desktop/io/input.txt", "r", stdin);
        freopen("/home/ankit/Desktop/io/output.txt", "w", stdout);
    #endif
}
int32_t main() {
   //IO();
    fastio;
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n; cin >> n;
    vector <int> ar(n + 1);
    for (int i = 1; i <= n; i++) {
    	cin >> ar[i];
    }
    int res = 1;
    sort(all(ar));
    for (int i = 1; i <= n; i++) {
    	res *= (ar[i] - ar[i - 1] + 1 + MOD);
    	res %= MOD;
    }
    cout << res << endl;

    return 0;
}


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

void find_sum(vector<ll>& A, ll n, vector<ll>& v)
{
    ll m=A.size();
    for(ll i=0;i<n;i++)
    {
        ll sum=0,x=i,j=m-1;
        while(x)
        {
            if(x&1)
                sum+=A[j];
            j--;
            x/=2;
        }
        v.push_back(sum);
    }
}
int main()
{
    ll N,w; cin>>N>>w;
    ll n=N/2,m=N-n;
    vector<ll> u(n),v(m);
    for(ll i=0;i<n;i++)
        cin>>u[i];
    for(ll i=0;i<m;i++)
        cin>>v[i];
    vector<ll> x,y;
    ll temp=pow(2,n);
    find_sum(u,temp,x);
    temp=pow(2,m);
    find_sum(v,temp,y);
    sort(y.begin(),y.end());
    ll ans=0;
    for(ll i:x)
    {
        if(w<i)
            continue;
        ll index=upper_bound(y.begin(),y.end(),w-i)-y.begin();
        index--;
        if(index!=-1 && i+y[index]>ans)
        {
            ans=i+y[index];
        }
    }
    cout<<ans;
}

#include <bits/stdc++.h>
#include <map>
#include <iostream>

using namespace std;
typedef long long ll;
typedef long double ld;
#define BIG 2005

set<ll> solve(vector<ll> B, ll T){
    set<ll> ans;
    for(int i=0;i<B.size();i++){
        if(B[i] > T) continue;
        set<ll> add;
        add.insert(B[i]);
        for(auto tm: ans){
            if(tm+B[i]<=T){
                add.insert(tm+B[i]);
            }
        }
        for(auto tm:add){
            ans.insert(tm);
        }
    }
    return ans;
}

int main() {
    int N;
    ll T;
    cin >> N >> T;
    vector<ll> B;
    vector<ll> C;
    for(int i=0;i<N;i++){
        ll a;
        cin >> a;
        if(i<N/2){
            B.push_back(a);
        }else{
            C.push_back(a);
        }
    }
    // B
    set<ll> ansb = solve(B, T);
    vector<ll> bans;
    for(auto an:ansb){
        bans.push_back(an); //昇順
    }
    // C
    set<ll> ansc = solve(C, T);
    // CとBを組み合わせる
    ll answer = 0;
    for(auto an:ansb){
        answer = max(answer, an);
    }
    for(auto an:ansc){
        if(bans.empty() || bans.size()==0){
            answer = max(answer, an);
            continue;
        }
        int l = 0;
        int r = bans.size()-1;
        if(bans[l]+an>T){
            answer = max(answer, an);
            continue;
        }
        if(bans[r]+an<=T){
            answer = max(answer, an+bans[r]);
            continue;
        }
        while(l+1!=r){
            int mid = (l+r)/2;
            if(an+bans[mid]<=T){
                l = mid;
            }else{
                r = mid;
            }
        }
        answer = max(answer, an+bans[l]);
    }
    printf("%lld\n", answer);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
#define rep(i,a,b) for(i=(a);i<=(b);++i)
#define per(i,a,b) for(i=(a);i>=(b);--i)
#define REP(i,a,b) for(i=(a);i< (b);++i)
#define PER(i,a,b) for(i=(a);i> (b);--i)
#define ERR(...) fprintf(stderr,__VA_ARGS__)
template<class T>inline void cmn(T &x,const T &a){if(a<x)x=a;}
template<class T>inline void cmx(T &x,const T &a){if(x<a)x=a;}
template<class T>inline bool bmn(T &x,const T &a){if(a<x){x=a;return true;}else return false;}
template<class T>inline bool bmx(T &x,const T &a){if(x<a){x=a;return true;}else return false;}

const int MAXN=100005;
char S[MAXN];
signed main(){
#ifdef ONLINE_JUDGE
	//freopen("problemname.in","r",stdin);
	//freopen("problemname.out","w",stdout);
#endif
	int N;
	scanf("%d%s",&N,S);
	if(S[0]!=S[N-1])puts("1");
	else{
		int i;
		REP(i,1,N-2)if(S[i]!=S[0]&&S[0]!=S[i+1]){
			puts("2");
			return 0;
		}
		puts("-1");
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int cnt[N];
int main() {
	string s;
	cin>>s;
	int f=0;
	int g=0;
	int x=0;
	if(s.size()==1) {
		if(s[0]=='8') {
			cout<<"Yes"<<endl;
			return 0;
		}
		else {
			cout<<"No"<<endl;
			return 0;
		}
	}
	else if(s.size()==2) {
		int cur = s[0]-'0';
		int cur1= s[1]-'0';
		if((cur*10+cur1)%8==0 || (cur1*10+cur)%8==0) {
			cout<<"Yes"<<endl;
			return 0;
		}
		else {
			cout<<"No"<<endl;
			return 0;
		}
	}
	string op = s;

for(int i = 0; i < op.size(); i++)//判断字符串有多少数字
    {
    	char tmp = op[i];
    	cnt[op[i] - '0']++;
	}
	int time = 100 / 8 + 1;
	int flg = 1;
	for(int i = time; 8 * i < 1000; i++)//枚举100-1000里8的倍数
	{
		flg = 1;
		int tmp = 8 * i;
		int tmpp[10] = {0};//8的倍数的数字组成
		while(tmp)
		{
			if(tmp%10 == 0)
			{
				flg = 0;
				break;
			}
			tmpp[tmp%10]++;
			tmp /= 10;
		}
		if(flg == 0) continue;
		for(int i = 1; i < 10; i++)
		{
			if(cnt[i] < tmpp[i])
			{
				flg = 0;
				break;
			}
		}
		if(flg)
		{
			break;
		}

	}
	if(flg)
	{
		cout << "Yes" << endl;
	}
	else
	{
		cout << "No" << endl;
	}
    return 0;
}


//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
template<typename T> using V = vector<T>;
template<typename T> using VV = vector<vector<T>>;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define siz(v) (ll)(v).size()
#define rep(i,a,n) for(ll i=a;i<(ll)(n);++i)
#define repr(i,a,n) for(ll i=n-1;(ll)a<=i;--i)
#define ENDL '\n'
typedef pair<int,int> Pi;
typedef pair<ll,ll> PL;
constexpr ll mod = 1000000007; // 998244353;
constexpr ll INF = 1000000099;
constexpr ll LINF = (ll)(1e18 +99);
const ld PI = acos((ld)-1);
const vector<ll> dx={-1,1,0,0},dy={0,0,-1,1};
template<typename T,typename U> inline bool chmin(T& t, const U& u){if(t>u){t=u;return 1;}return 0;}
template<typename T,typename U> inline bool chmax(T& t, const U& u){if(t<u){t=u;return 1;}return 0;}
template<typename T> inline T gcd(T a,T b){return b?gcd(b,a%b):a;}

template<typename T,typename Y> inline T mpow(T a, Y n) {
  T res = 1;
  for(;n;n>>=1) {
    if (n & 1) res = res * a;
    a = a * a;
  }
  return res;
}

template <typename T> V<T> prefix_sum(const V<T>& v) {
  int n = v.size();
  V<T> ret(n + 1);
  rep(i, 0, n) ret[i + 1] = ret[i] + v[i];
  return ret;
}

template<typename T>
istream& operator >> (istream& is, vector<T>& vec){
  for(auto&& x: vec) is >> x;
  return is;
}

template<typename T,typename Y>
ostream& operator<<(ostream& os,const pair<T,Y>& p){
  return os<<"{"<<p.fs<<","<<p.sc<<"}";
}

template<typename T> ostream& operator<<(ostream& os,const V<T>& v){
  os<<"{";
  for(auto e:v)os<<e<<",";
  return os<<"}";
}

template<typename ...Args>
void debug(Args&... args){
  for(auto const& x:{args...}){
    cerr<<x<<' ';
  }
  cerr<<ENDL;
}

template <long long MOD = 1000000007>  // MODを適宜設定すること
struct Mint {
  using M = Mint;
  ll a;
  Mint(ll x = 0) {
    a = x % MOD;
    if(a < 0) a += MOD;
  }  // Mintで

  M pow(ll n) {
    M b = *this, r = 1 % MOD;
    while(n) {
      if(n & 1) r *= b;
      b *= b;
      n >>= 1;
    }
    return r;
  }

  M inv() { return pow(MOD - 2); }

  M& operator+=(M r) {
    a += r.a;
    if(a >= MOD) a -= MOD;
    return *this;
  }
  M& operator-=(M r) {
    a += MOD - r.a;
    if(a >= MOD) a -= MOD;
    return *this;
  }
  M& operator*=(M r) {
    a = 1LL * a * r.a % MOD;
    return *this;
  }
  M& operator/=(M r) { return (*this) *= r.inv(); }
  M operator+(M r) const { return M(a) += r; };
  M operator-(M r) const { return M(a) -= r; };
  M operator*(M r) const { return M(a) *= r; };
  M operator/(M r) const { return M(a) /= r; };
  M operator-() const { return M() - *this; }

  friend ostream& operator<<(ostream& os, const M& r) { return os << r.a; }
};
using M = Mint<mod>;

M sum(ll x){
  ll res=x;
  res*=x+1;
  res/=2;
  return res%mod;
}

M sum2(ll x,ll lim){
  if(x<=lim)return sum(x);
  M res=sum(lim);
  res+=lim*(x-lim);
  return res;
}

signed main(){
  cin.tie(0);cerr.tie(0);ios::sync_with_stdio(false);
  cout<<fixed<<setprecision(20);
  int qq;cin>>qq;
  while(qq--){
    ll n,a,b;cin>>n>>a>>b;

    if(a+b>n){
      cout<<0<<ENDL;
      continue;
    }
    if(a<b)swap(a,b);

    M ans=0;
    ans+=sum2((n*2-a-b+2)/2,n-a+1)*sum(n-a-b+1);
    ans+=(sum2((n*2-a-b+1)/2,n-a+1)-sum(n-a-b+1))*sum(n-a-b+1);
    ans*=4;

    cout<<ans<<ENDL;
  }

}
//! ( . _ . ) !
//CHECK overflow,vector_size,what to output?

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+7;
int main()
{
    int t;
    scanf("%lld",&t);
    while(t--)
	{
        ll a,b,c,ans=0;
        scanf("%lld%lld%lld",&a,&b,&c);
        if(a>=b+c){
            ll res=(a-b-c+1)*(a-b-c+2)%mod;
            ll num=2*(a-b+1)*(a-c+1)%mod-res;
            if(num<0) num=(mod-(-num)%mod)%mod;
            ans=res*num%mod;
        }
        cout<<ans<<endl;
    }
}


/**
*    author:  shu8Cream
*    created: 26.04.2021 09:06:13
**/

#include <bits/stdc++.h>
#include <chrono>
using namespace std;
#define rep(i,n) for (int i=0; i<(n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using P = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
using namespace std::chrono;
inline double get_time_sec(void){
    return static_cast<double>(duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count())/1000000000;
}

int si,sj;
vvi t(50,vi(50));
vvi p(50,vi(50));

string finans = "";
int finscore = 0;

const vi di = {1,0,-1,0};
const vi dj = {0,1,0,-1};

void input(){
    cin >> si >> sj;
    rep(i,50)rep(j,50) cin >> t[i][j];
    rep(i,50)rep(j,50) cin >> p[i][j];
}

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

    input();

    set<int> allrem;
    int loop = 20;
    while(loop--){
        double start = get_time_sec();
        int score = 0;
        string ans = "";
        vector<P> remcell;

        while(get_time_sec() - start<0.098){

            int tscore = 0;
            string tans = "";
            set<int> rem;
            queue<P> pq;
            pq.push({si,sj});
            int i=0,j=0;
            while(!pq.empty()){
                int pi=i,pj=j;
                tie(i,j) = pq.front(); pq.pop();
                remcell.push_back({i,j});
                rem.insert(t[i][j]);
                tscore += p[i][j];
                queue<P> emp;
                swap(pq,emp);

                if(!(i == si && j == sj)){
                    if(i-pi==1) tans+="D";
                    else if(i-pi==-1) tans+="U";
                    else if(j-pj==1) tans+="R";
                    else if(j-pj==-1) tans+="L";
                }

                // cout << ans << endl;

                vi rand = {0,1,2,3};

                std::random_device seed_gen;
                std::mt19937 engine(seed_gen());
                shuffle(all(rand),engine);
                for(auto k : rand){
                    int ni = i + di[k];
                    int nj = j + dj[k];
                    if(ni<0 || ni>=50 || nj<0 || nj>=50) continue;
                    if(t[i][j]==t[ni][nj]) continue;
                    if(rem.count(t[ni][nj])) continue;
                    if(allrem.count(t[ni][nj])) continue;

                    int ngcnt = 0;
                    rep(kk,4){
                        int nni = ni + di[kk];
                        int nnj = nj + dj[kk];
                        if(nni<0 || nni>=50 || nnj<0 || nnj>=50) {ngcnt++; continue;}
                        if(t[nni][nnj]==t[ni][nj]) {ngcnt++; continue;}
                        if(rem.count(t[nni][nnj])) {ngcnt++; continue;}
                        if(allrem.count(t[nni][nnj])) {ngcnt++; continue;}
                    }
                    if(ngcnt==4) continue;

                    pq.push({ni,nj});
                }
            }

            if(tscore>score){
                score = tscore;
                ans = tans;
            }

        }

        int anslen = (ans.size()+1)/2;
        ans = ans.substr(0,anslen);

        finans += ans;

        allrem.insert(t[si][sj]);
        rep(k,anslen){
            if(ans[k]=='U') si--;
            if(ans[k]=='D') si++;
            if(ans[k]=='L') sj--;
            if(ans[k]=='R') sj++;
            allrem.insert(t[si][sj]);
        }
    }
    cout << finans << endl;
}

#include <bits/stdc++.h>
//#include <atcoder/all>

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VPLL;
typedef vector<bool> VB;
typedef priority_queue<LL> PQ_DESC;
typedef priority_queue<LL, VL, greater<LL>> PQ_ASC;
typedef priority_queue<PII> PQ_DESC_PII;
typedef priority_queue<PII, vector<PII>, greater<PII>> PQ_ASC_PII;
typedef priority_queue<VL> PQ_DESC_VL;
typedef priority_queue<VL, vector<VL>, greater<VL>> PQ_ASC_VL;
typedef priority_queue<PLL> PQ_DESC_PLL;
typedef priority_queue<PLL, vector<PLL>, greater<PLL>> PQ_ASC_PLL;

#define ALL(c) (c).begin(),(c).end()
#define PB push_back
#define MP make_pair
#define SORT_ASC(c) sort(ALL(c))
//#define SORT_DESC(c) sort(ALL(c), greater<typeof(*((c).begin()))>())
#define SORT_DESC(c) sort((c).rbegin(),(c).rend())
#define REV(c) reverse((c).begin(), (c).end())
#define SIZE(a) LL((a).size())

#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define ROF(i,a,b) for(LL i=(b-1);i>=(a);--i)
#define REP(i,n) FOR(i,0,n)
#define PER(i,n) ROF(i,0,n)

const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL LARGE_INT = 1e9+100;
const LL INF = 2e9+100;
const LL INF_LL = (LL)INF*(LL)INF;
const LL MOD = 1e9+7;

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

LL TIME_LIMIT = 1990;

LL start_time;
string result_string;
LL result_point=0;
LL h=50;
LL w=50;
VVL t;
VVL p;

void check(string result, LL point){
    if(point > result_point){
        result_point = point;
        result_string = result;
    }
}

void dfs(LL x, LL y, VB v, string result, LL point){
    LL time = chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count();
    if(time-start_time > TIME_LIMIT){
        check(result, point);
        return;
    }

    v[t[x][y]]=true;

    VL dx={1,-1,0,0};
    VL dy={0,0,1,-1};

    VVL tp;

    REP(i,4){
        LL nx = x+dx[i];
        LL ny = y+dy[i];
        if(nx >= h || nx < 0 || ny >= w || ny < 0){
            continue;
        }
        if(v[t[nx][ny]]){
            continue;
        }

        tp.PB({p[nx][ny],i,nx,ny});
    }

    if(tp.empty()){
        check(result, point);
        return;
    }

    SORT_DESC(tp);

    for(auto w : tp){
        LL np = w[0];
        LL i = w[1];
        LL nx = w[2];
        LL ny = w[3];
        //cerr<<np<<" "<<i<<" "<<nx<<" "<<ny<<endl;
        string d;
        if(i==0){
            d = "D";
        }
        if(i==1){
            d = "U";
        }
        if(i==2){
            d = "R";
        }
        if(i==3){
            d = "L";
        }
        dfs(nx, ny, v, result+d, point+np);
    }
}

void Main()
{
    start_time = chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count();
    //dump(start_time)
    LL si,sj;cin>>si>>sj;

    t.resize(h);
    REP(i,h){
        t[i].resize(w);
        REP(j,w){
            cin>>t[i][j];
        }
    }
    p.resize(h);
    REP(i,h){
        p[i].resize(w);
        REP(j,w){
            cin>>p[i][j];
        }
    }

    VB v(h*w);
    string result = "";
    LL point = 0;

    dfs(si, sj, v, result, point);

    cout<<result_string<<endl;

	return;
}

int main()
{
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	Main();
    return 0;
}


#include <bits/stdc++.h>
#include <random>
using namespace std; typedef unsigned long long _ulong; typedef long long int lint; typedef pair<lint, lint> plint; typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(lint 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 endk '\n'
struct fast_ios { fast_ios() { cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
template<class T> auto add = [](T a, T b) -> T { return a + b; };
template<class T> auto f_max = [](T a, T b) -> T { return max(a, b); };
template<class T> auto f_min = [](T a, T b) -> T { return min(a, b); };
template<class T> using V = vector<T>;
using Vl = V<lint>; using VVl = V<Vl>;
template< typename T > ostream& operator<<(ostream& os, const vector< T >& v) {
	for (int i = 0; i < (int)v.size(); i++) os << v[i] << (i + 1 != v.size() ? " " : "");
	return os;
}
template< typename T >istream& operator>>(istream& is, vector< T >& v) {
	for (T& in : v) is >> in;
	return is;
}
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; }
lint gcd(lint a, lint b) { if (b == 0) return a; else return gcd(b, a % b); }
lint ceil(lint a, lint b) { return (a + b - 1) / b; }
lint digit(lint a) { return (lint)log10(a); }
lint dist(plint a, plint b) { return abs(a.first - b.first) * abs(a.first - b.first) + abs(a.second - b.second) * abs(a.second - b.second); }
lint m_dist(plint a, plint b) { return abs(a.first - b.first) + abs(a.second - b.second); }
void Worshall_Floyd(VVl& g) { REP(k, SZ(g)) REP(i, SZ(g)) REP(j, SZ(g)) chmin(g[i][j], g[i][k] + g[k][j]); }
const lint MOD = 1e9 + 7, INF = 5e18;
lint dx[8] = { 1, 0, -1, 0, 1, -1, 1, -1 }, dy[8] = { 0, 1, 0, -1, -1, -1, 1, 1 };
bool YN(bool flag) { cout << (flag ? "YES" : "NO") << endk; return flag; } bool yn(bool flag) { cout << (flag ? "Yes" : "No") << endk; return flag; }
struct Edge {
	lint from, to;
	lint cost;
	Edge(lint u, lint v, lint c) {
		cost = c;
		from = u;
		to = v;
	}
	bool operator<(const Edge& e) const {
		return cost < e.cost;
	}
};
struct WeightedEdge {
	lint to;
	lint cost;

	WeightedEdge(lint v, lint c = 1) {
		to = v;
		cost = c;
	}
	bool operator<(const WeightedEdge& e) const {
		return cost < e.cost;
	}
};
using WeightedGraph = V<V<WeightedEdge>>;
typedef pair<plint, lint> tlint;
typedef pair<plint, plint> qlint;
typedef pair<string, lint> valstring;

lint N, M, u, v;
int main() {
	cin >> N >> M;
	VVl mat(N, Vl(N, 0));
	REP(i, M) {
		cin >> u >> v; u--; v--;
		mat[u][v] = 1;
		mat[v][u] = 1;
	}
	Vl dp(1 << N, INF);
	V<plint> vec;
	FOR(mask, 1, 1 << N) {
		bool flag = true;
		REP(i, N) {
			FOR(j, i + 1, N) {
				if (((mask >> i) % 2) && (mask >> j) % 2) {
					if (mat[i][j] == 0) flag = false;
				}
			}
		}
		lint cnt = 0;
		REP(i, N) {
			if ((mask >> i) % 2 == 1) cnt++;
		}
		if (flag) {
			dp[mask] = 1;
		}
		vec.push_back({ cnt, mask });
	}
	sort(ALL(vec));
	for(plint v : vec) {
		lint mask = v.second;
		for (lint bit = mask; ; bit = (bit - 1) & mask) {
			if (!(dp[bit] == INF || dp[mask ^ bit] == INF)) {
				chmin(dp[mask], dp[bit] + dp[mask ^ bit]);
			}
			if (bit == 0) break;
		}
	}
	cout << dp[(1ll << N) - 1] << endk;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 19, msz = 1 << sz;

int n, m, g[sz][sz], dp[msz];
bitset <msz> can;

int main() {
   cin >> n >> m;
   while(m--) {
      int u, v;
      scanf("%d %d", &u, &v);
      u--, v--;
      g[u][v] = g[v][u] = 1;
   }
   int lim = 1 << n;
   can.set();
   for(int i=1; i<lim; i++) {
      vector <int> nodes;
      for(int j=0; j<n; j++) if(i & (1 << j)) nodes.push_back(j);
      for(int &x : nodes) {
         for(int &y : nodes) {
            if(x ^ y and !g[x][y]) can[i] = 0;
         }
      }
   }

   for(int i=1; i<lim; i++) {
      dp[i] = n;
      for(int j=i; j>0; j=(j-1)&i)
         if(can[j]) dp[i] = min(dp[i], 1 + dp[i ^ j]);
   }
   cout << dp[lim-1];
}


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

#define int long long
vector<int> a;
int n;
long double f(long double x){
	long double sum = 0;
	for(int i = 0; i < n; i++){
		sum += (x + a[i] - min(2 * x, (long double)a[i]));
		// if(x == 1.5)
			// cout << x << " " << a[i] << " " << min(2 * x, (long double)a[i]) << endl;
	}
	// if(x == 1.5)
	// cout << x << " " << sum << endl;
	return sum / n;
}
int32_t main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> n;
	a.resize(n);
	for(int i = 0; i < n; i++)
		cin >> a[i];
	long double eps = 1e-6;
	long double l = 0, r = 2000000000;
    while (r - l > eps) {
        double m1 = l + (r - l) / 3;
        double m2 = r - (r - l) / 3;
        long double f1 = f(m1);
        long double f2 = f(m2);
        if (f1 > f2)
            l = m1;
        else
            r = m2;
    }
    printf("%0.7Lf\n", f(l));
    // cout << std::setprecision(8) << f(l) << endl;

}

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define P pair<int,int>
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()
#define pb push_back
template<class T,class U>
inline bool chmax(T &a,U b){
    if(a<b){
        a=b;
        return true;
    }
    return false;
}
template<class T,class U>
inline bool chmin(T &a,U b){
    if(a>b){
        a=b;
        return true;
    }
    return false;
}
constexpr int INF=1000000000;
constexpr ll llINF=1000000000000000000;
constexpr int mod=1000000007;
constexpr double eps=1e-8;
const double pi=acos(-1);
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int Random(int mi,int ma){
    random_device rnd;
    mt19937 mt(rnd());//32bit
    //[mi,ma]
    uniform_int_distribution<int>engine(mi,ma);
    return engine(mt);
}
/*
vector<vector<ll>>C,sC;
void init_comb(int n,int m){
    C.resize(n+1,vector<ll>(m+1,0));
    sC.resize(n+1,vector<ll>(m+1,0));
    C[0][0]=1;
    for(int i=1;i<=n;i++){
        C[i][0]=1;
        for(int j=1;j<=m;j++){
            C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
        }
    }
    rep(i,n+1){
        rep(j,m){
            sC[i][j+1]=(sC[i][j]+C[i][j])%mod;
        }
    }
}*/
ll gcd(ll a,ll b){
    while(a%b){
        a%=b;
        swap(a,b);
    }
    return b;
}
ll lcm(ll a,ll b){
    return a/gcd(a,b)*b;
}
bool isprime(int a){
    if(a==1)return false;
    for(int i=2;i*i<=a;i++){
        if(a%i==0)return false;
    }
    return true;
}
set<int>primes;
void init_prime(int n){
	primes.insert(2);
    for(int i=3;i<=n;i+=2){
        bool f=true;
        for(int j:primes){
            if(j*j>i)break;
            if(i%j==0){
                f=false;
                break;
            }
        }
        if(f)primes.insert(i);
    }
}
ll modpow(ll a,ll b){
    ll res=1;
    while(b){
        if(b&1){
            res*=a;
            res%=mod;
        }
        a*=a;
        a%=mod;
        b>>=1;
    }
    return res;
}
vector<ll>inv,fact,factinv;
void init_fact(int n){
    inv.resize(n+1);
    fact.resize(n+1);
    factinv.resize(n+1);
    inv[0]=0;
    inv[1]=1;
    fact[0]=1;
    factinv[0]=1;
    for(ll i=1;i<=n;i++){
        if(i>=2)inv[i]=mod-((mod/i)*inv[mod%i]%mod);
        fact[i]=(fact[i-1]*i)%mod;
        factinv[i]=factinv[i-1]*inv[i]%mod;
    }
}
ll _inv(ll a,ll m=mod){
    //gcd(a,m) must be 1
    ll b=m,u=1,v=0;
    while(b){
        ll t=a/b;
        a-=t*b;swap(a,b);
        u-=t*v;swap(u,v);
    }
    u%=m;
    if(u<0)u+=m;
    return u;
}
ll comb(int a,int b){
    if(a<b)return 0;
    if(a<0)return 0;
    return fact[a]*factinv[a-b]%mod*factinv[b]%mod;
}
ll multicomb(int a,int b){
    return comb(a+b-1,b);
}
struct BIT{
    int n,beki=1;
    vector<int>bit;
    BIT(int x){
        bit.resize(x+1,0);
        n=x;
        while(beki*2<=n)beki*=2;
    }
    int sum(int i){
        int res=0;
        while(i>0){
            res+=bit[i];
            i-=i&-i;
        }
        return res;
    }
    int sum(int l,int r){
        //[l,r]
        return sum(r)-(l==0?0:sum(l-1));
    }
    int get(int l){
        return sum(l,l);
    }
    void set(int i,int x){
        add(i,x-get(i));
    }
    void add(int i,int x){
        while(i<=n){
            bit[i]+=x;
            i+=i&-i;
        }
    }
    void add(int l,int r,int x){
        //[l,r]+=x
        add(l,x);
        if(r<n)add(r+1,-x);
    }
    int lowerbound(int w){
        if(w<=0)return 0;
        int x=0;
        for(int k=beki;k>0;k>>=1){
            if(x+k<=n&&bit[x+k]<w){
                w-=bit[x+k];
                x+=k;
            }
        }
        return x+1;
    }
};
int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    int n,a[300010];
    cin>>n;
    rep(i,n){
        cin>>a[i];
        a[i]++;
    }
    BIT bit(n);
    ll ans=0;
    rep(i,n){
        ans+=bit.sum(a[i]+1,n);
        bit.add(a[i],1);
    }
    cout<<ans<<endl;
    rep(i,n-1){
        ans+=(n-a[i]-a[i]+1);
        cout<<ans<<endl;
    }
    return 0;
}


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

const int MAXN = 1e5, INF = 1e9;
int n, m, vis[MAXN], dis[17][17], gems[17];
vector<int> adj[MAXN];

int bfs(int start, int find) {
	int dis = INF;
	queue<pair<int, int>> q;
	q.push({start, 0});
	vis[start] = 1;
	while(!q.empty()) {
		auto curr = q.front();
		if(curr.first == find) {
			dis = curr.second;
			break;
		}
		q.pop();
		for(int next: adj[curr.first]) {
			if(!vis[next]) {
				q.push({next, curr.second+1});
				vis[next] = 1;
			}
		}
	}
	return dis;
}

int main() {
	cin >> n >> m;
	for(int i=0; i<m; i++) {
		int a, b;
		cin >> a >> b;
		a--, b--;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	int k;
	cin >> k;
	for(int i=0; i<k; i++) {
		cin >> gems[i];
		gems[i]--;
	}
	for(int i=0; i<k; i++) {
		for(int j=i+1; j<k; j++) {
			memset(vis, 0, sizeof(vis));
			int res = bfs(gems[i], gems[j]);
			dis[i][j] = res, dis[j][i] = res;
			if(dis[i][j] == INF) {
				cout << -1 << endl;
				return 0;
			}
		}
	}
	int dp[(1<<k)][k];
	for(int i=0; i<(1<<k); i++) {
		for(int j=0; j<k; j++) {
			dp[i][j] = INF;
		}
	}
	for(int i=0; i<k; i++) {
		dp[(1<<i)][i] = 1;
	}
	for(int i=0; i<(1<<k); i++) {
		for(int j=0; j<k; j++) {
			if(!(i>>j&1)) continue;
			for(int prev=0; prev<k; prev++) {
				if(!(i>>prev&1) || prev == j) continue;
//				cout << i << ' ' << j << ' ' << prev << endl;
				dp[i][j] = min(dp[i][j], dp[i^(1<<j)][prev]+dis[prev][j]);
			}
		}
	}
	int ans = INF;
	for(int i=0; i<k; i++) {
		ans = min(ans, dp[(1<<k)-1][i]);
	}
	cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(),(v).end()
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; }
const ll INF=1LL<<60;
const int inf=(1<<30)-1;
const int mod=1e9+7;
int dx[8]={1,0,-1,0,-1,-1,1,1};
int dy[8]={0,1,0,-1,-1,1,-1,1};
const int nmax=100005;
vector<vector<pair<ll,ll>>> e(nmax);
ll d[nmax];
void dijkstra(ll s){
  priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> pq;
  fill(d,d+nmax,INF);
  d[s]=0;
  pq.push(make_pair(0,s));
  while(!pq.empty()){
    pair<ll,ll> p=pq.top();
    pq.pop();
    ll v=p.second;
    if(d[v]<p.first){
      continue;
    }
    for(auto x:e[v]){
      if(d[x.first]>d[v]+x.second){
        d[x.first]=d[v]+x.second;
        pq.push(make_pair(d[x.first],x.first));
      }
    }
  }
}
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,m;cin >> n >> m;
    vector<int> a(m),b(m);
    REP(i,m){
        cin >> a[i] >> b[i];
        a[i]--,b[i]--;
        e[a[i]].push_back({b[i],1});
        e[b[i]].push_back({a[i],1});
    }
    int k;cin >> k;
    vector<int> c(k);
    REP(i,k){
        cin >> c[i];
        c[i]--;
    }
    vector<vector<ll>> dis(k,vector<ll>(k));
    REP(i,k){
        dijkstra(c[i]);
        REP(j,k) dis[i][j]=d[c[j]];
    }
    vector<vector<ll>> dp(1<<k,vector<ll>(k,INF));
    REP(i,k) dp[1<<i][i]=1;
    REP(t,1<<k){
        REP(i,k){
            if(dp[t][i]==INF) continue;
            if(!((t>>i)&1)) continue;
            REP(j,k){
                if((t>>j)&1) continue;
                chmin(dp[t|(1<<j)][j],dp[t][i]+dis[i][j]);
            }
        }
    }
    ll ans=INF;
    REP(i,k) chmin(ans,dp[(1<<k)-1][i]);
    cout << (ans==INF?-1:ans) << endl;
}


#include <bits/stdc++.h>
#define przxct ""
#define fto(i,j,h)    for (int i=j; i<=h; ++i)
#define maxn
#define ll long long
#define pi 3.141592653589

using namespace std;

string s;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> s;
    int n = s.length();
    s = " " + s;
    for (int i = 1; i <= n; i += 2)
    {
        if (s[i] < 'a' || s[i] > 'z')
        {
            cout << "No";
            return 0;
        }
    }
    for (int i = 2; i <= n; i += 2)
    {
        if (s[i] < 'A' || s[i] > 'Z')
        {
            cout << "No";
            return 0;
        }
    }
    cout << "Yes";
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
using lli = long long;
#define rep(i,n) for(int i=0;i<n;i++)
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; }
template<class T>
ostream &operator<<(ostream &os, const vector<T> &x){
    os << "{";
    for(size_t i = 0; i < x.size(); i++){
        if(i < x.size()-1) os << x[i] << ", ";
        else os << x[i];
    }
    os << "}";
    return os;
}

int main(void){
    int n;
    cin >> n;
    n-=1;
    n/=100;
    cout << n+1 << endl;

    return 0;
}


#include <bits/stdc++.h>
#define FOR(i, n) for(int i = 0; i < (n); ++i)
#define REP(i, a, b) for(int i = (a); i < (b); ++i)
#define TRAV(i, a) for(auto & i : (a))
#define SZ(x) ((int)(x).size())
#define X first
#define Y second
#define MP std::make_pair
#define PR std::pair
typedef long long ll;
typedef std::pair<int, int> PII;
typedef std::vector<int> VI;

int n;
std::vector<PII> A;
std::vector<PII> B;

int check(int i){
	if(A[i].X == -1 || A[i].Y == -1) return -1;
	return A[i].Y-A[i].X;
}

bool can(int a, int b){
	int len = (b-a+1);
	if(len%2 != 0) return false;
	int dl = len/2;
	FOR(i, dl){
		if(B[a+i].X == -1 && B[a+i+dl].X == -1) continue;
		if(B[a+i].Y == 0) return false;
		if(B[a+i+dl].Y == 1) return false;
		if(B[a+i].X != -1 && B[a+i+dl].X != -1 && B[a+i].X != B[a+i+dl].X) return false;
	}
	return true;
}

int main(){
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(0);
	std::cin >> n;
	A.resize(n);
	B.resize(2*n+1, MP(-1, -1));

	std::set<int> set;
	int cnt = 0;
	FOR(i, n){
		std::cin >> A[i].X >> A[i].Y;
		if(A[i].X != -1 && A[i].Y != -1 && A[i].X >= A[i].Y){
			std::cout << "No\n";
			return 0;
		}
		if(A[i].X != -1) B[A[i].X] = MP(i, 1), set.insert(A[i].X), cnt++;
		if(A[i].Y != -1) B[A[i].Y] = MP(i, 0), set.insert(A[i].Y), cnt++;
	}
	if(SZ(set) != cnt){
		std::cout << "No\n";
		return 0;
	}
	std::vector<bool> dp(2*n+1);
	dp[0] = true;
	REP(i, 1, 2*n+1){
		FOR(j, i){
			dp[i] = (dp[i] || (dp[j] && can(j+1, i)));
		}
	}

	std::cout << (dp[2*n] ? "Yes\n" : "No\n");

	return 0;
}

//LIBRARIES
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <chrono>
//MACROS
#define _crt_secure_no_warnings
#define FREEELO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define pb push_back
#define mp make_pair
#define INF 0x3F3F3F3F
#define MAX 100001
#define MAXLL 1000000000000000000LL
#define MOD 1000000007
#define LOGMAX 20
#define ALPHABET 128
#define loop(i, n) for (int i = 1; i <= (n); i++)
#define loop0(i, n) for (int i = 0; i < (n); i++)
#define sz(x) (int)x.size()
#define all(v) v.begin(), v.end()
#define all1(v) v.begin() + 1, v.end()
#define F first
#define S second
#define EPS 1e-6
using namespace std;
//TYPE DEFINITIONS
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef pair<ll, ll> llll;
typedef pair<int, int> ii;
typedef unordered_map<int, int> umii;
typedef unordered_map<ll, ll> umll;
typedef unordered_set<int> usi;
typedef unordered_multiset<int> usmi;
//main exec

int main(){
    FREEELO
    string s; cin >> s; int n = sz(s); s = " " + s;
    vector<int> dp(n + 1);
    for (int i = 1; i <= n; i++){
        dp[i] = dp[i - 1] + (s[i] - '0');
    }
    if (dp[n] % 3 == 0) { cout << 0 << endl; return 0; }
    for (int k = 1; k <= n - 1; k++){
        for (int i = 1; i + k - 1 <= n; i++){
            int cur_sum = dp[i - 1] + dp[n] - dp[i + k - 1];
            if (cur_sum % 3 == 0) {
                cout << k << endl; return 0;
            }
        }
    }
    cout << -1 << endl;
}


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

int main()
{
	string a;
	getline(cin, a ,'.');
	cout << a;
	return 0;
}

#include <bits/stdc++.h>
using namespace std ;
int main(){
    int n ;
    cin >> n ;
    if(n%100 == 0){
        cout << 100 << endl ;
    }else{
        cout << 100 - (n%100) << endl ;
    }
    return 0 ;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
// For debug
// Ref: https://qiita.com/ysuzuki19/items/d89057d65284ba1a16ac
#define dump(var)  do{std::cerr << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cerr << e << "\n";}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cerr << e << " "; } std::cerr << "\n";}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ std::cerr << "\n"; for(const auto& v : vv){ view(v); } }
template<typename T> void dump_cout(const T& v) { for(long long i = 0; i < v.size(); i++) std::cout << v[i] << (i == v.size()-1 ? "\n" : " "); }

void solve() {
    ll n, m;
    cin >> n >> m;
    vector<vector<bool>> G(n, vector<bool>(n, false));
    rep(i, 0, m) {
        ll a, b;
        cin >> a >> b;
        a--; b--;
        G[a][b] = G[b][a] = true;
    }
    vector<bool> ok(1<<n, true);
    rep(bit, 0, 1<<n) {
        vector<ll> vs;
        rep(i, 0, n) if(bit & (1<<i)) vs.push_back(i);
        rep(i, 0, vs.size()) {
            if(!ok[bit]) break;
            rep(j, i+1, vs.size()) {
                if(!ok[bit]) break;
                if(!G[vs[i]][vs[j]]) ok[bit] = false;
            }
        }
    }
    const ll inf = 1LL<<60;
    vector<ll> dp(1<<n, inf);
    dp[0] = 0;
    rep(bit, 0, 1<<n) {
        if(dp[bit] >= inf) continue;
        ll cbit = (1<<n) - 1 - bit;
        for(ll add = cbit; ; add = (add-1) & cbit) {
            if(!add) break;
            if(ok[add]) dp[bit|add] = min(dp[bit|add], dp[bit]+1);
        }
    }
    cout << dp[(1<<n)-1] << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}

#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
#include <cmath>
#include <complex>
#include <functional>
#include <numeric>
#include <iomanip>
#include <cassert>
#include <random>
#include <chrono>

/* #include <atcoder/all> */
/* using namespace atcoder; */
using namespace std;

void debug_out(){ cout << "\n"; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cout << H << " ";
  debug_out(T...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

#define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for(int i=(a);i<(n);++i)
#define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i)
#define REP(i, n) for(int i=0;i<int(n);++i)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define IN(a, x, b) (a<=x && x<b)
#define OUT(a, x, b) (x<a || b<=x)
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; }

#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll MOD = 1000000007;
/* const ll MOD = 998244353; */
const ll INF = 1ll<<60;
const double PI = acos(-1);

struct INIT { INIT(){
  cin.tie(0); ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
}}INIT;

vector<int> compress(vector<int> &v){
  int N = v.size();
  vector<int> ret = v;
  sort(ALL(ret));
  ret.erase(unique(ALL(ret)), ret.end());
  REP(i, N){
    v[i] = lower_bound(ALL(ret), v[i])-ret.begin();
  }
  return ret;
}

// a -> b -> cの最短距離
int f(int a, int b, int c){
  return abs(b-a) + abs(c-b);
}

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

  vector<int> X(N), C(N);
  REP(i, N) cin >> X[i] >> C[i];

  vector<int> c = compress(C);

  REP(i, C.size()){
    C[i]++;
  }

  int MAX = 0;
  REP(i, N) chmax(MAX, C[i]);
  MAX++;

  vector<int> r(MAX, -INF), l(MAX, INF);
  REP(i, N){
    chmax(r[C[i]], X[i]);
    chmin(l[C[i]], X[i]);
  }
  r[0] = 0;
  l[0] = 0;

  // dp[i][j]: i番目の左右に来た時の最小値
  vector<vector<int>> v(MAX, vector<int>(2, INF));
  v[0][0] = 0;
  v[0][1] = 0;
  FOR(i, 1, MAX){
    chmin(v[i][0], v[i-1][0]+f(r[i-1], l[i], r[i]));
    chmin(v[i][0], v[i-1][1]+f(l[i-1], l[i], r[i]));

    chmin(v[i][1], v[i-1][0]+f(r[i-1], r[i], l[i]));
    chmin(v[i][1], v[i-1][1]+f(l[i-1], r[i], l[i]));
  }

  int ans = min(v[MAX-1][0]+abs(r[MAX-1]), v[MAX-1][1]+abs(l[MAX-1]));

  cout << ans << "\n";

  return 0;
}


#include <bits/stdc++.h>
const int N = 3e5+5;
int n, q, t[N], a[N];
int ask(int x)
{
    int y = 0;
    for( ; x; x -= x&-x) y ^= t[x];
    return y;
}
void add(int x, int y)
{
    for( ; x <= n; x += x&-x) t[x] ^= y;
}
int main()
{
    scanf("%d%d", &n, &q);
    for(int i = 1; i <= n; ++i) scanf("%d", a+i), a[i] ^= a[i-1];
    while(q--)
    {
        int op, x, y; scanf("%d%d%d", &op, &x, &y);
        if(op == 1) add(x, y);
        else printf("%d\n", a[y]^a[x-1]^ask(y)^ask(x-1));
    }
    return 0;
}

#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define mt make_tuple
#define MOD 1000000007
#define fo(i,a,b) for(i=a;i<b;i++)
#define foe(i,a,b) for(i=a;i<=b;i++)
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector <long long int>
#define pii pair <int,int>
#define pll pair <long long int, long long int>
#define vpii vector< pair<int,int> >
#define vpll vector < pair <long long int,long long int> >
#define boost ios::sync_with_stdio(false); cin.tie(0)
using namespace std;
const int inf = 1e9 + 5;
const ll inf64 = 1e18 + 5;

template <typename T>
class segtree {
private:
	vector<T> value;
	int size;

public:
	const T INITIAL_VALUE = 0;
	const T NEUTRAL = 0;

	segtree (int n) {
		size = 1;
		while(size < n) size *= 2;
		value.assign(2 * size, INITIAL_VALUE);
	}

	T calc_op(T a, T b) {
		return (a ^ b);
	}

	void build(vector<T> &v, int x, int lx, int rx) {
		if(rx - lx == 1) {
			if(lx < (int)v.size()) value[x] = v[lx];
			return;
		}

		int m = (lx + rx) / 2;
		build(v, 2 * x + 1, lx, m);
		build(v, 2 * x + 2, m, rx);
		value[x] = calc_op(value[2 * x + 1], value[2 * x + 2]);
	}

	void upd(int i, T v, int x, int lx, int rx) {
		if(rx - lx == 1) {
			value[x] = v;
			return;
		}

		int m = (lx + rx) / 2;
		if(i < m) upd(i, v, 2 * x + 1, lx, m);
		else upd(i, v, 2 * x + 2, m, rx);
		value[x] = calc_op(value[2 * x + 1], value[2 * x + 2]);
	}

	T qry(int l, int r, int x, int lx, int rx) {
		if(l >= rx || lx >= r) return NEUTRAL;
		if(l <= lx && rx <= r) return value[x];

		int m = (lx + rx) / 2;
		T s1 = qry(l, r, 2 * x + 1, lx, m);
		T s2 = qry(l, r, 2 * x + 2, m, rx);
		return calc_op(s1, s2);
	}

	void build(vector<T> &v) {
		build(v, 0, 0, size);
	}

	void upd(int i, T v) {
		upd(i, v, 0, 0, size);
	}

	T qry(int l, int r) {
		return qry(l, r, 0, 0, size);
	}
};

int main()
{
	boost;
	int n, q;
	cin >> n >> q;

	vector <int> a(n);
	for(int i = 0; i < n; i++)
	cin >> a[i];

	segtree<int> st(n);
	st.build(a);

	while(q--) {
		int t;
		cin >> t;
		if(t == 1) {
			int x, y;
			cin >> x >> y; --x;
			st.upd(x, (a[x] ^ y));
			a[x] ^= y;
		}
		else {
			int l, r;
			cin >> l >> r; --l;
			cout << st.qry(l, r) << '\n';
		}
	}
}


// Problem: B - RGB Matching
// Contest: AtCoder - NOMURA Programming Contest 2021(AtCoder Regular Contest 121)
// URL: https://atcoder.jp/contests/arc121/tasks/arc121_b
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
#define pb push_back
#define mp make_pair
const double PI = 4*atan(1);
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9+7;
const int MAX_N = 2e6+10;

int n;
ll a[MAX_N];
char c[MAX_N];

void solve()
{
	map<int,vector<ll>> x;
	for(int i=0;i<2*n;++i){
		if(c[i]=='R') x[0].pb(a[i]);
		else if(c[i]=='B')x[1].pb(a[i]);
		else x[2].pb(a[i]);
	}
	vector<int> res;
	for(int i=0;i<3;++i){
		sort(x[i].begin(),x[i].end());
		if((int)x[i].size()%2) res.pb(i);
	}
	if(res.empty()){
		cout<<"0\n";
		return;
	}
	int u = res[0], v = res[1];
	auto cal = [&] (int u, int v) {
		ll ans = LINF;
		if(x[v].empty()) return ans;
		for(int i=0,j=0;i<(int)x[u].size();++i){
			while(j+1<(int)x[v].size() && x[v][j+1]<=x[u][i]) j++;
			ans = min(ans, abs(x[u][i]-x[v][j]));
			if(j+1<(int)x[v].size()){
				ans = min(ans, abs(x[u][i]-x[v][j+1]));
			}
		}
		return ans;
	};
	int w = 3-u-v;
	ll ans = min(cal(u,v),cal(u,w)+cal(v,w));
	cout<<ans;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cin >> n;
	for(int i=0;i<2*n;++i)cin>>a[i]>>c[i];
	solve();
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 20;
int x[N], y[N], z[N];
int g[N][N];
int f[N][1 << N], n;
int get_dis(int a, int b) {
	return abs(x[a] - x[b]) + abs(y[a] - y[b]) + max(0ll, z[b] - z[a]);
}
signed main() {
#ifndef ONLINE_JUDGE
	freopen("E:\\in.txt", "r", stdin);
#endif
	cin >> n;
	int M = 1 << (n - 1);
	memset(f, 0x3f, sizeof f);
	for (int i = 0; i < n; i++) {
		cin >> x[i] >> y[i] >> z[i];
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			g[i][j] = get_dis(i, j);
		}
	}
	for (int i = 0; i < n; i++) {
		f[i][0] = g[i][0];
	}
	for (int j = 1; j < M; j++) {
		for (int i = 0; i < n; i++) {
			if ((j >> (i - 1)) & 1) continue;
			for (int k = 1; k < n; k++) {
				if (i == k) continue;
				if ((j >> (k - 1) & 1) == 0) continue;
				f[i][j] = min(f[i][j], g[i][k] + f[k][j ^ (1 << (k - 1))]);
			}
		}
	}
	cout << f[0][M - 1] << endl;
}


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

vector<bool> seen;
void dfs(const Graph &G, int v) {
    seen[v] = true;

    for (auto next_v : G[v]) {
        if (seen[next_v]) continue;
        dfs(G, next_v);
    }
}

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

    Graph G(N);
    for (int i = 0; i < M; ++i) {
        int a, b;
        cin >> a >> b;
        G[a-1].push_back(b-1);
    }
    int ans = 0;
    rep(i,N){
            seen.assign(N, false);
            dfs(G, i);
        rep(j,N){
            if(seen.at(j)){
                ans++;
            }
        }
    }
    cout << ans << endl;

}

#include<bits/stdc++.h>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define m_p make_pair
#define pb push_back
#define fi first
#define se second
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++)
using namespace std;
const int N=2010;
int n,m;
vector<int>g[N];
int vis[N];
int ans=0;
void dfs(int u){
	vis[u]=1;
	ans++;
	rep(i,g[u].size()){
		int v=g[u][i];
		if(vis[v])continue;
		dfs(v);
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin>>n>>m;
	rep(i,m){
		int u,v;
		cin>>u>>v;
		u--;v--;
		g[u].pb(v);
	}
	rep(i,n){
		memset(vis,0,sizeof(vis));
		dfs(i);
	}
	cout<<ans<<"\n";
	return 0;
}


// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
// #include <atcoder/all>

using namespace std;
// using namespace atcoder;

// using ll = long long;
// int : -2**31 〜 2**31-1 (2**31=2147483648=2*10**9)
// long long : -2**63 〜 2**63-1 (2**63=9223372036854775808=9*10**18)

using P = pair<int,int>;

// #define int ll
// int main() --> signed main() として使う．

#define rep(i,n) for (int i=0; i<n; i++)
#define rep2(i,s,e) for (int i=s; i<e; i++)

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

const int INF = (int)2e9;

struct Edge {
    int cost;
    int to;
    Edge(int c, int t) : cost(c), to(t) {}
};

void dijkstra_heap(int s, vector<vector<Edge>> &edges, vector<int> &dist) {
    dist[s] = 0;
    priority_queue<P,vector<P>,greater<P>> q;
    q.push(make_pair(dist[s],s));
    while (!q.empty()) {
        int v = q.top().second;
        int d = q.top().first;
        q.pop();
        if (d>dist[v]) continue;
        for (auto e: edges[v]) {
            if (chmin(dist[e.to],dist[v]+e.cost)) {
                q.push(make_pair(dist[e.to],e.to));
            }
        }
    }
}

int no(int i, int j, int C) {
        return i*C + j;
}

int main() {
    cin.tie(nullptr);
    // 小数点以下12桁まで
    cout << fixed << setprecision(12);
    // input
    int R,C;
    cin >> R >> C;
    vector<vector<int>> a(R,vector<int>(C-1));
    vector<vector<int>> b(R-1,vector<int>(C));
    vector<int> dist(R*C,INF);
    vector<vector<Edge>> edges(R*C);
    rep(i,R) rep(j,C-1) cin >> a[i][j];
    rep(i,R-1) rep(j,C) cin >> b[i][j];
    // edges
    rep(i,R) {
        rep(j,C) {
            // left
            if (j>0) edges[no(i,j,C)].push_back(Edge(a[i][j-1],no(i,j-1,C)));
            // right
            if (j<C-1) edges[no(i,j,C)].push_back(Edge(a[i][j],no(i,j+1,C)));
            // down
            if (i<R-1) edges[no(i,j,C)].push_back(Edge(b[i][j],no(i+1,j,C)));
            // up
            rep2(k,1,i+1) {
                edges[no(i,j,C)].push_back(Edge(1+k,no(i-k,j,C)));
            }
        }
    }
    // calc
    dijkstra_heap(no(0,0,C),edges,dist);
    // output
    // rep(i,R) {
    //     rep(j,C) {
    //         cout << dist[no(i,j,C)] << " ";
    //     }
    //     cout << endl;
    // }
    int ans = dist[no(R-1,C-1,C)];
    cout << ans << endl;
}

#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define double long double
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define endl '\n'
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define rev(a) a.rbegin(),a.rend()
#define all(a) a.begin(),a.end()
#define setbits(x) (__builtin_popcount(x))
#define pi pair<int,int>
#define precise(a,k) cout<<fixed<<setprecision(k)<<a;
#define fill(s,x,n) vector<s> x(n); rep(i,0,n) cin>>x[i];
#define minpq(type) priority_queue<type,vector<type>,greater<type>>
#define debug(...) cerr<<"{ "<<#__VA_ARGS__<<" = "<<(__VA_ARGS__)<<" }"<<endl;
#define debugp(...) cerr<<"{ "<<#__VA_ARGS__<<" = "<<"("<<(__VA_ARGS__).ff<<","<<(__VA_ARGS__).ss<<")"<<" }"<<endl;
#define debugv(...) cerr<<"{ "; for(int i=0; i<size(__VA_ARGS__); i++) cerr<<#__VA_ARGS__<<"["<<i<<"]"<<" = "<<(__VA_ARGS__)[i]<<" ; "; cerr<<"}"<<endl;
#define debugpv(...) cerr<<"{ "; for(int i=0; i<size(__VA_ARGS__); i++) cerr<<#__VA_ARGS__<<"["<<i<<"]"<<" = "<<"("<<(__VA_ARGS__)[i].ff<<","<<(__VA_ARGS__)[i].ss<<")"<<" ; "; cerr<<"}"<<endl;
#define mod 998244353
#define inf 1000000007
#define inf64  1000000000000000005
#define PI 3.141592653589793

void solve(){
    int n,m;
    cin>>n>>m;
    fill(int,a,m);
    if(m==0){
        cout<<1;
        return;
    }
    sort(all(a));
    vector<int> v;
    int t = 1;
    int prev = 0;
    for(int x: a){
        if(x-prev>1){
            v.push_back(x-prev-1);
            t+=x-prev;
        }
        prev = x;
    }
    if(n-prev>0){
        v.push_back(n-prev);
         t+=n-prev;
    }
   // debugv(v)
    if(v.size()<=1){
        cout<<v.size();
        return;
    }
    sort(all(v));
    int ans = 0;
    for(int i=0; i<(int)v.size(); i++){
        ans+=v[i]/v[0] + (bool)(v[i]%v[0]);
    }
    cout<<ans;
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    /*
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    */
    int test;
   // cin>>test;
    test=1;
    //sieve();
    //int cse=1;
    while(test--){
       // cout<<"Case #"<<cse<<": ";
        solve();
        cout<<endl;
        //cse++;
    }
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int Inf = 2000000030;
constexpr long long INF= 2000000000000000000;

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

template<typename T>
T modpow(T N, T P, T M){
    if(P < 0) return 0;
    if(P == 0) return 1;
    if(P % 2 == 0){
        T t = modpow(N, P/2, M);
        if(M == -1) return t * t;
        return t * t % M;
    }
    if(M == -1) return N * modpow(N,P - 1,M);
    return N * modpow(N, P-1, M) % M;
}

ll MOD = 1000000007;

long long fac[2000010], finv[2000010], inv[2000010];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < 2000010; i++) {
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k) {
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
    int N,M,K;
    cin >> N >> M >> K;
    if(N > M + K) {
        cout << 0 << endl;
        return 0;
    }
    COMinit();
    long long ret = COM(N + M,N);
    ret -= COM(N + M,N - K - 1);
    if(ret < 0) ret += MOD;
    cout << ret << endl;
}

//@Author: KeinYukiyoshi

// clang-format off
#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx")
#define int long long
using namespace std;
#define stoi stoll
#define fi first
#define se second
#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; i++)
#define rep2(i, a, b) for (int i = (int)(a), i##_len=(b); i < i##_len; i++)
#define rep3(i, a, b) for (int i = (int)(a), i##_len=(b); i >= i##_len; i--)
#define FOR(i, a) for (auto &i: a)
#define ALL(obj) begin(obj), end(obj)
#define _max(x) *max_element(ALL(x))
#define _min(x) *min_element(ALL(x))
#define _sum(x) accumulate(ALL(x), 0LL)

const int MOD   = 1000000007;
// const int MOD    = 998244353;
// const int INF    = (int)1e18;
// const int INF    = 10000000000007; // 1e13 + 7
// const int INF    = LLONG_MAX; // 9.2e18
const double EPS = 1e-8;
const double PI  = 3.14159265358979;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
template <class T> using VVV = vector<vector<vector<T>>>;
template <class T, class S>  using P = pair<T, S>;
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 (b < a) {a = b;return true;}return false;}
int _ceil(int a, int b) { return (a >= 0 ? (a + (b - 1)) / b : (a - (b - 1)) / b); }
template<class T> T chmod(T &a, T mod=MOD) {a =  a >= 0 ? a % mod : a - (mod * _ceil(a, mod)); return a;};
int _mod(int a, int mod=MOD) {return a >= 0 ? a % mod : a - (mod * _ceil(a, mod));}
int _pow(int a, int b) {int res = 1;for (a %= MOD; b; a = a * a % MOD, b >>= 1)if (b & 1) res = res * a % MOD;return res;}
struct mint {long long x;mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}mint operator-() const { return mint(-x); }mint &operator+=(const mint a) {if ((x += a.x) >= MOD) x -= MOD;return *this;}mint &operator-=(const mint a) {if ((x += MOD - a.x) >= MOD) x -= MOD;return *this;}mint &operator*=(const mint a) { (x *= a.x) %= MOD;return *this; }mint operator+(const mint a) const { return mint(*this) += a; }mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; }mint pow(long long t) const {if (!t) return 1;mint a = pow(t >> 1);a *= a;if (t & 1) a *= *this;return a;}mint inv() const { return pow(MOD - 2); }mint &operator/=(const mint a) { return *this *= a.inv(); }mint operator/(const mint a) const { return mint(*this) /= a; }};istream &operator>>(istream &is, mint &a) { return is >> a.x; }ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
// clang-format on

class ACompetition {
public:
  static void solve(istream &cin, ostream &cout) {
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    int X, Y, Z;
    cin >> X >> Y >> Z;
    cout << (Y * Z - 1) / X << endl;
  }
};

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


//fake_name
#include<bits/stdc++.h>
using namespace std ;
#define int long long int
#define F first
#define S second

int MOD = 998244353 ;
int mod = 1e9 + 7 ;
int inf = 1e18 ;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

signed main()
{
    ios_base::sync_with_stdio(false) ;
    cin.tie(0) ; cout.tie(0) ;
    int n , x ;
    cin >> n >> x ;
    for( int i = 0 ; i < n ; i++ ){
    	int y ;
    	cin >> y ;
    	if( y != x )
    		cout << y << ' ' ;
    }
    cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
}

#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef long double ld;
/*
#include <chrono>
using namespace std::chrono;
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<pair<lli,lli>, null_type,less<pair<lli,lli>>, rb_tree_tag,tree_order_statistics_node_update>
*/
#define endl "\n"
#define pb push_back
const lli mod=1e9+7;
const lli mod1=998244353;
#define fir first
#define sec second
#define plli pair<lli,lli>
/*
lli power(lli a, lli b) {
    lli res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}
lli powermod(lli a, lli b)
{
    lli res = 1;
    while (b > 0) {
        if (b & 1)
            res = ((res%mod)*(a%mod))%mod;
        a = (a * a)%mod;
        b >>= 1;
    }
    return res;
}
*/
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lli T;
    T=1;
    //cin>>T;
    while(T--)
    {
        lli n,m;
        cin>>n>>m;
        if(m%n==0)
            cout<<"Yes\n";
        else
            cout<<"No\n";
    }
    return 0;
}


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

int main() {
  int n;
  cin >> n;
  vector<string> s(n);
  vector<int> t(n);
  rep(i,n){
    cin >> s[i] >> t[i];
  }
  int mx = 0;
  rep(i,n){
    mx = max(t[i], mx);
  }
  int mx2 = 0;
  string name;
  rep(i,n){
    if(t[i] == mx){
      continue;
    }
    if(t[i] > mx2){
      mx2 = t[i];
      name = s[i];
    }
  }
  cout << name << endl;

  return 0;
}

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
int N;
int A[1000];
int B[1000];
int dist[1000][1000];
int doit(int k){
  if(k >= N){
    int i;
    for(i=(30)-1;i>=(0);i--){
      if(((k) &(1<<(i)))){
        k ^= (1<<i);
        break;
      }
    }
  }
  return k;
}
int main(){
  int i;
  rd(N);
  for(i=(0);i<(N);i++){
    A[i] = doit(2 * i);
    B[i] = doit(2 * i + 1);
  }
  for(i=(0);i<(N);i++){
    wt_L(A[i]+1);
    wt_L(' ');
    wt_L(B[i]+1);
    wt_L('\n');
  }
  return 0;
}
// cLay version 20201206-1

// --- original code ---
// int N;
// int A[1000], B[1000];
//
// int dist[1000][1000];
//
// int doit(int k){
//   if(k >= N) rrep(i,30) if(BIT_ith(k,i)) k ^= (1<<i), break;
//   return k;
// }
//
// {
//   rd(N);
//   rep(i,N){
//     A[i] = doit(2 * i);
//     B[i] = doit(2 * i + 1);
//   }
//   rep(i,N) wt(A[i]+1, B[i]+1);
//
//   // rep(i,N) rep(j,N) dist[i][j] = int_inf;
//   // rep(i,N) dist[i][A[i]] <?= 1;
//   // rep(i,N) dist[i][B[i]] <?= 1;
//   // rep(i,N) dist[i][i] = 0;
//   // rep(k,N) rep(i,N) rep(j,N) dist[i][j] <?= dist[i][k] + dist[k][j];
//   // int mx = 0;
//   // rep(i,N) rep(j,N) mx >?= dist[i][j];
//   // wt("mx", mx);
// }


///Bismillahir Rahmanir Rahim
#include "bits/stdc++.h"

#define                           ll                          long long
#define                           int                         ll
#define                           fi                          first
#define                           si                          second
#define                           mp                          make_pair
#define                           pb                          push_back
#define                           pi                          pair<int,int>
#define                           node(a,b,c)                 mp(mp(a,b),c)
#define                           clr(x)                      memset(x,0,sizeof(x));
#define                           f(i,l,r)                    for(int i=l;i<=r;i++)
#define                           rf(i,r,l)                   for(int i=r;i>=l;i--)
#define                           done(i)                     cout<<"done = "<<i<<endl;
#define                           show(x,y)                   cout<<x<<" : ";for(auto z:y)cout<<z<<" ";cout<<endl;
#define                           fast                        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;

const ll inf=1e18;
const int mod=1e9+7;
const int M=12;
int a[M][M];
 main()

{
    fast
    int n,k;
    cin>>n>>k;
    f(i,1,n)
    {
        f(j,1,n)
        {
            cin>>a[i][j];
        }
    }
    int ses=0;
    vector<int>vec;
    f(i,2,n)vec.pb(i);
    sort(vec.begin(),vec.end());
    do
    {
       int tym=a[1][vec[0]]+a[vec[n-2]][1];
       int sz=n-1;
       for(int i=1;i<sz;i++)
       {
           int x=vec[i-1];
           int y=vec[i];
           tym+=(a[x][y]);
       }

       if(tym==k)ses++;

    }while(next_permutation(vec.begin(),vec.end()));

    cout<<ses<<"\n";
    return 0;

}

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi=vector<int>;

int main() {
    int N,K; cin>>N>>K;
    vector<vector<ll>> T(N,vector<ll>(N));
    for (int i = 0; i < N; i++){
        for (int j = 0; j < N; j++){
            cin>>T[i][j];
        }
    }
    vi v;
    for (int i = 1; i < N; i++)v.push_back(i);
    int ans=0;
    do{
        ll tmp=0;
        tmp+=T[0][v[0]];
        for (int i = 0; i < N-2; i++){
            tmp+=T[v[i]][v[i+1]];
        }
        tmp+=T[v[N-2]][0];
        if(tmp==K)ans++;
    }while(next_permutation(v.begin(),v.end()));
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
#define pi acos(-1)
#define ll long long
using namespace std;
void solve();
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int q=1;//cin>>q;
    while(q--){
        solve();
     }
  return 0;
}
void solve(){
   int n;cin>>n;
   int a[n],p[n],x[n],ans=INT_MAX;
  for(int i=0;i<n;i++){
    cin>>a[i]>>p[i]>>x[i];
    if(x[i]-a[i]>0){
      ans=min(ans,p[i]);
    }
  }
  if(ans==INT_MAX)
    cout<<-1<<endl;
  else
    cout<<ans<<endl;
}

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

#define ll long long
#define print(a)          \
    for (auto x : a)      \
        cout << x << " "; \
    cout << endl
#define print_upto(a, n)        \
    for (ll i = 1; i <= n; i++) \
        cout << a[i] << " ";    \
    cout << endl
#define take(a, n)              \
    for (ll i = 1; i <= n; i++) \
        cin >> a[i];

#define watch(x) cout << (#x) << " is " << (x) << "\n"
#define watch2(x, y) cout << (#x) << " is " << (x) << " and " << (#y) << " is " << (y) << "\n"
#define watch3(x, y, z) cout << (#x) << " is " << (x) << " and " << (#y) << " is " << (y) << " and " << (#z) << " is " << (z) << "\n"

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

#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

#define ff first
#define ss second
#define null NULL
#define all(c) (c).begin(), (c).end()
#define nl "\n"

#define ld long double
#define eb emplace_back
#define pb push_back
#define pf push_front
#define MOD 1000000007
#define inf 1e17
// cout << fixed << setprecision(9) << ans << nl;

typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef map<ll, ll> mll;

const ll N = 200009;

void solve()
{
    ll n;
    cin >> n;

    vector<ll> v;
    for (ll i = 1; i <= n; i++)
    {
        ll a, p, x;
        cin >> a >> p >> x;

        x = x - a;
        if (x > 0)
        {
            v.pb(p);
        }
    }

    sort(all(v));

    if (v.size() == 0)
    {
        cout << -1 << nl;
    }
    else
    {
        cout << v[0] << nl;
    }
}

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    ll T = 1;
    // cin >> T;

    while (T--)
    {
        solve();
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
using namespace std;

using VLL = vector <long long>;
using VVLL = vector <VLL>;

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

int main() {
    long long A;
    long long B;
    cin >> A >> B;

    VLL primesList = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
    31, 37, 41, 43, 47, 53, 59, 61, 67, 71}; // p <= 72
    unordered_map <long long, long long> pMask;

    for (long long n = A; n <= B; ++n) {
        pMask[n] = 0;
        for (int j = 0; j < primesList.size(); ++j) {
            if (gcd(n, primesList[j]) != 1) {
                pMask[n] |= 1<<j;
            }
        }
    }

    int UPPER = 1 << primesList.size();
    // cout << "DEBUG SIZE " << primesList.size() << endl;
    VVLL dp(2, VLL(UPPER));
    dp[0][0] = dp[1][0] = 1;

    for (long long n = A; n <= B; ++n) {
        for (int i = 0; i < UPPER; ++i) {
            if ((i & pMask[n]) == 0) {
                dp[1][i | pMask[n]] += dp[0][i];
            }
        }
        dp[0] = dp[1];
    }

    long long ans = 0;
    for (int i = 0; i < UPPER; ++i) {
        ans += dp[0][i];
    }

    cout << ans << endl;

    return 0;
}

#include <bits/stdc++.h>

#define ln                 '\n'
#define all(dat)           dat.begin(), dat.end()
#define loop(i, to)        for (__typeof(to) i = 0; i < to; ++i)
#define cont(i, to)        for (__typeof(to) i = 1; i <= to; ++i)
#define circ(i, fm, to)    for (__typeof(to) i = fm; i <= to; ++i)
#define foreach(i, dat)    for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long          num;

using namespace std;

const int nsz = 74;
const int hsz = int(1e7) + 39, esz = 2e6;
bitset<nsz> g[nsz + 5];
int vis[nsz + 5], pw[nsz + 5];
num n, fm, to, ans = 1, a[nsz + 5];

struct hash_table {
	int sz, hd[hsz + 5];

	struct node {
		int nxt;
		bitset<nsz> w;

		node() {}
		node(int nxt, bitset<nsz> w): nxt(nxt), w(w) {}
	} e[esz + 5];

	int inline to_key(const bitset<nsz> &w) {
		int u = 0;
		cont (i, n) u = (u * 2 + w[i]) % hsz;
		return u;
	}

	void inline ins(const bitset<nsz> &w, int u) {
		e[++sz] = node(hd[u], w), hd[u] = sz;
	}

	bool inline qry(const bitset<nsz> &w, int u) {
		for (int i = hd[u]; i; i = e[i].nxt) {
			if (e[i].w == w) return 1;
		}
		return 0;
	}
};
hash_table dat;

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

void bfs(int s, int &sz, int *nd) {
	static int q[nsz + 5];
	int ql = 0, qr = 0;
	for (q[qr++] = s, vis[s] = s; ql != qr;) {
		int u = q[ql++];
		nd[sz++] = u;
		cont (v, n) if (g[u][v] && !vis[v]) q[qr++] = v, vis[v] = s;
	}
}

num inline calc(int s) {
	static int nd[nsz + 5], d[esz + 5];
	static bitset<nsz> q[esz + 5], p[esz + 5];
	int sz = 0, ql = 0, qr = 1, cnt = 0;
	bfs(s, sz, nd);
	if (sz == 1) return 2;
	for (; ql != qr; ++cnt) {
		bitset<nsz> cur = q[ql], ban = p[ql], nban;
		int key = d[ql]; ++ql;
		loop (i, sz) if (!ban[nd[i]]) {
			int v = nd[i], nkey = (key + pw[v]) % hsz;
			cur[v] = 1, nban = ban | g[v];
			if (!dat.qry(cur, nkey)) {
				q[qr] = cur, p[qr] = nban, d[qr] = nkey; ++qr;
				dat.ins(cur, nkey);
			}
			cur[v] = 0;
		}
	}
	return cnt;
}

void inline init() {
	pw[0] = 1;
	cont (i, n) pw[i] = pw[i - 1] * 2 % hsz;
}

int main() {
	scanf("%lld%lld", &fm, &to);
	n = to - fm + 1;
	circ (i, fm, to) a[num(i) - fm + 1] = i;
	cont (u, n) cont (v, u) g[u][v] = g[v][u] = gcd(a[u], a[v]) > 1;
	init();
	cont (u, n) if (!vis[u]) ans *= calc(u);
	printf("%lld\n", ans);
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M=2e5+1;
struct BIT{
	ll bit[200004];
	int dat[200004];
	void init(){
		for(int i=0;i<=M;i++)bit[i]=dat[i]=0;
	}
	void add(int id,int a,int b){
		for(;id;id-=(id&-id))dat[id]+=a,bit[id]+=b;
	}
	ll got1(int id){
		ll res=0;
		for(;id<=M;id+=(id&-id))res+=bit[id];
		return res;
	}
	int got2(int id){
		int res=0;
		for(;id<=M;id+=(id&-id))res+=dat[id];
		return res;
	}
}X,Y;
vector<int>all;
int n,m,Q;
ll ans=0;
int a[200005],b[200004];
int x[200004],y[200004],t[200004];
ll gotx(int t){
	return Y.got1(t)+(ll)(m-Y.got2(t))*all[t];
}
ll goty(int t){
	return X.got1(t)+(ll)(n-X.got2(t))*all[t];
}
int main(){
	X.init(),Y.init();
	cin>>n>>m>>Q;
	all.push_back(0);
	for(int i=1;i<=Q;i++){
		cin>>t[i]>>x[i]>>y[i],all.push_back(y[i]);
	}
	all.push_back(-1);
	sort(all.begin(),all.end()),all.erase(unique(all.begin(),all.end()),all.end());
	for(int i=1;i<=n;i++)a[i]=1,X.add(1,1,0);
	for(int i=1;i<=m;i++)b[i]=1,Y.add(1,1,0);
	for(int i=1;i<=Q;i++){
		y[i]=lower_bound(all.begin(),all.end(),y[i])-all.begin();
		if(t[i]==1){
			ans-=gotx(a[x[i]]);X.add(a[x[i]],-1,-all[a[x[i]]]);
			a[x[i]]=y[i];
			ans+=gotx(a[x[i]]);X.add(a[x[i]],1,all[a[x[i]]]);
		}else{
			ans-=goty(b[x[i]]);Y.add(b[x[i]],-1,-all[b[x[i]]]);
			b[x[i]]=y[i];
			ans+=goty(b[x[i]]);Y.add(b[x[i]],1,all[b[x[i]]]);
		}
		cout<<ans<<"\n";
	}
}

// Jai Shree Ram

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ook order_of_key
#define fbo find_by_order

#define rep(i,a,n)     for(int i=a;i<n;i++)
#define ll             long long
#define int            long long
#define pb             push_back
#define all(v)         v.begin(),v.end()
#define endl           "\n"
#define x              first
#define y              second
#define gcd(a,b)       __gcd(a,b)
#define mem1(a)        memset(a,-1,sizeof(a))
#define mem0(a)        memset(a,0,sizeof(a))
#define sz(a)          (int)a.size()
#define pii            pair<int,int>
#define hell           1000000007
#define elasped_time   1.0 * clock() / CLOCKS_PER_SEC

template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.x>>a.y;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.x<<" "<<a.y;return out;}
template<typename T,typename T1>T maxs(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T mins(T &a,T1 b){if(b<a)a=b;return a;}

const int N=1e9+5;
const int M=1e7;

struct node{
	int l;int r;
	int val;
	int cnt;
}t[M];
int cnt=0;

int n;

void extend(int v){
	if(t[v].l==0){
		t[v].l=++cnt;
		t[v].r=++cnt;
	}
}

void pushup(int v){
	t[v].val = t[t[v].l].val + t[t[v].r].val;
	t[v].cnt = t[t[v].l].cnt + t[t[v].r].cnt;
}

void update(int v,int l,int r,pii val,int tl=0,int tr=1e9){
	if(tl>r || l>tr)return;
	if(l<=tl && tr<=r){
		assert(tl == tr);
		t[v].val += val.x;
		t[v].cnt += val.y;
		return;
	}
	extend(v);
	int mid=(tl+tr)/2;
	update(t[v].l,l,r,val,tl,mid);
	update(t[v].r,l,r,val,mid+1,tr);
	pushup(v);
}

pii query(int v,int l,int r,int tl=0,int tr=1e9){
	if(tl>r || l>tr)return {0,0};
	if(l<=tl && tr<=r){
		return {t[v].val,t[v].cnt};
	}
	int mid=(tl+tr)/2;
	extend(v);
	pii a =query(t[v].l,l,r,tl,mid);
	pii b =query(t[v].r,l,r,mid+1,tr);
	return {a.x+b.x,a.y+b.y};
}

int solve(){
		int n,m,q; cin >> n >> m >> q;

		int ans = 0;
		int roota = ++cnt;
		int rootb = ++cnt;

		vector<int>v1(n+1),v2(m+1);
		update(roota,0,0,{0,n});
		update(rootb,0,0,{0,m});

		rep(i,0,q){
			int t; cin >> t;
			int x,y; cin >> x >> y;
			if(t == 1){
				if(y > v1[x]){
					auto s = query(rootb,0,y);
					ans += s.y*y;
					s = query(rootb,v1[x]+1,y);
					ans -= s.x;
					s = query(rootb,0,v1[x]);
					ans -= v1[x]*s.y;
				}
				else if(y < v1[x]){
					auto s = query(rootb,0,v1[x]);
					ans -= s.y*v1[x];
					s = query(rootb,0,y);
					ans += s.y*y;
					s = query(rootb,y+1,v1[x]);
					ans += s.x;
				}
				update(roota,v1[x],v1[x],{-v1[x],-1});
				update(roota,y,y,{y,1});
				v1[x] = y;
			}
			else{
				if(y > v2[x]){
					auto s = query(roota,0,y-1);
					ans += s.y*y;
					s = query(roota,0,v2[x]-1);
					ans -= s.y*v2[x];
					s = query(roota,v2[x],y-1);
					ans -= s.x;
				}
				else if(y < v2[x]){
					auto s = query(roota,0,v2[x]-1);
					ans -= s.y*v2[x];
					s = query(roota,0,y-1);
					ans += s.y*y;
					s = query(roota,y,v2[x]-1);
					ans += s.x;
				}
				update(rootb,v2[x],v2[x],{-v2[x],-1});
				update(rootb,y,y,{y,1});
				v2[x] = y;
			}
			cout << ans << endl;
		}
 return 0;
}
signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #ifdef SIEVE
    sieve();
    #endif
    #ifdef NCR
    init();
    #endif
    int t=1;//cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>

#define For(i,j,k)  for(int i=j;i<=k;++i)

#define Dow(i,j,k)  for(int i=k;i>=j;--i)

#define ll long long

#define pb push_back

#define fir first

#define sec second

#define pb push_back

#define pa pair<int,int>

#define mk make_pair

using namespace std;

inline ll read()

{

    ll t=0,dp=1;char c=getchar();

    while(!isdigit(c))  {if(c=='-') dp=-1;c=getchar();}

    while(isdigit(c))   t=t*10+c-48,c=getchar();

    return t*dp;

}

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

inline void writeln(ll x){write(x);puts("");}

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

const int N=3005;

int n,k;

int dp[N][N],sum[N][N],mo=998244353;;

int main()

{

	n=read();k=read();	if(n==k)	{cout<<1<<endl;return 0;}

	For(i,1,k)	dp[i][i]=sum[i][i]=1;

	For(i,1,k)	For(j,1,i)	sum[i][j]=1;

	For(i,1,n-k)

	{

		Dow(j,1,i-1)

		{

			dp[i][j]=sum[i-j][(j+1)/2];

			sum[i][j]=(sum[i][j+1]+dp[i][j])%mo;

		}

	}

	int ans=0;

	For(j,1,n-k)	ans=(ans+dp[n-k][j])%mo;

	writeln(ans);

}

#include <cstdio>
#include <algorithm>

typedef long long LL;
const int Mod = 998244353;
const int MN = 3005;

int N, K, f[MN][MN];

int main() {
	scanf("%d%d", &N, &K), N -= K;
	f[0][0] = 1;
	for (int i = 1; i <= N; ++i) {
		f[i][0] = 0;
		for (int j = 1; j <= i; ++j)
			f[i][j] = (f[i][j - 1] + f[i - j][std::min(i - j, 2 * j)]) % Mod;
	}
	printf("%d\n", f[N][std::min(N, K)]);
	return 0;
}


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

int main() {

  int N;
  cin >> N;

  int A;
  cin >> A;

  int B;
  cin >> B;

cout << N - A + B << endl;

}

#include<bits/stdc++.h>

typedef  long long ll;
//forループ
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
//Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i,n) for(ll i=0;i<ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)
//
//

using namespace std;

int ctoi(char c) {
 switch (c) {
 case '0': return 0;
 case '1': return 1;
 case '2': return 2;
 case '3': return 3;
 case '4': return 4;
 case '5': return 5;
 case '6': return 6;
 case '7': return 7;
 case '8': return 8;
 case '9': return 9;
 default: return 0;
 }
}

 long long a,b,c,s,t,x,y,z;
 long long A,B,C,S,T,X,Y,Z;

 long long cnt;

 long double pi=3.14159265358979323846;

 string N,M;

unsigned long long D;

ll gcd(ll a,ll b){
	if(a==1 || b==1) return 1;
	if(a==0) return b;
	return gcd(b%a,a);
}

int main(){

	cin>>x>>a>>b;
	cout<<x-a+b;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(v) v.begin(),v.end()
#define run ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0);
#define mod 1000000007
#define decimal(k) cout<<fixed<<setprecision(k)<<endl
#define LL_MAX LLONG_MAX
#define yes cout<<"Yes"<<endl
#define no cout<<"No"<<endl
int exp(int x,int y){int res=1;x=x%mod;while(y>0){if(y&1)res=(res*x)%mod;y=y>>1;x=(x*x)%mod;}return res;}
int modinv(int x){return exp(x,mod-2);}
int add(int a,int b){a%=mod,b%=mod;a=((a+b)%mod+mod)%mod;return a;}
int sub(int a,int b){a%=mod,b%=mod;a=((a-b)%mod+mod)%mod;return a;}
int mul(int a,int b){a%=mod,b%=mod;a=((a*b)%mod+mod)%mod;return a;}
int gcd(int a, int b){if (b == 0)return a; return gcd(b, a % b);}
int Log2n( int n){ return (n > 1) ? 1 + Log2n(n / 2) : 0;}
int fac[100009];int ncr_mod(int n,int k){int ans=fac[n];ans*=modinv(fac[k]);ans%=mod;ans*=modinv(fac[n-k]);ans%=mod;return ans;}
vector<int>v_prime;void Sieve(int n){bool prime[n + 1];memset(prime,true,sizeof(prime));for (int p = 2; p*p <=n;p++){if(prime[p] ==true) {for(int i = p*p; i<= n; i += p)prime[i]=false;}}for(int p = 2;p<= n;p++)if (prime[p])v_prime.pb(p);}
vector<int>v_factor;void factors(int n){ for (int i=1; i<=sqrt(n); i++) {if (n%i == 0) {if (n/i == i) v_factor.pb(i);else { v_factor.pb(i),v_factor.pb(n/i);};} } sort(all(v_factor)); }
void out(vector<int>&a){for(int i=0;i<a.size();i++) cout<<a[i]<<" ";  cout<<endl;}

bool convertOpposite(string str)
{
    int ln = str.length();

    // Conversion according to ASCII values
    for (int i = 0; i < ln; i++) {

             if(i%2==0)
             {
               if (str[i] >= 'a' && str[i] <= 'z')
               {

               }

                 else
                 return false;
             }

             if(i%2==1)
             {
                    if (str[i] >= 'A' && str[i] <= 'Z')
                    {

                    }

                    else
                    return false;

             }
    }
    return true;
}

signed main()
 {

       run;

/*
open for mod factorial

     fac[0]=1;
       for(int i=1;i<100009;i++)
      {
         fac[i]=fac[i-1]*i;
          fac[i]%=mod;
      }
 */

/*
for sieve open this and use v_prime

       int pp=pow(10,6)+100000;
          Sieve(pp);

*/

 // USE v_factor For calculating factors

int  t=1;
 //cin>>t;
 while(t--)
{
         // vector< pair <int,int> > vp;
        //  map<int,int>mp; // x.second-->frequency
       //   set<int>st;

          int  n,i,x,y,ok=0,sum=0,ans=0,j,k,cnt=0,m,c=0;
          int  h[100009]={0};
          string s;
          cin>>s;
          if(convertOpposite(s)==true)
            yes;

            else
            no;

}
}

// Etavioxy
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#define il inline
#define ll long long
#define rep(i,s,t) for(register int i=(s);i<=(t);i++)
#define rev_rep(i,s,t) for(register int i=(s);i>=(t);i--)
#define each(i,u) for(int i=head[u];i;i=bow[i].nxt)
#define file(s) freopen(s".in" ,"r",stdin),freopen(s".out","w",stdout)
#define pt(x) putchar(x)
using namespace std;

const int mod = 1e9+7;

il ll qpow(ll a,ll b){
	ll ans= 1;
	for(; b; b>>=1,a=a*a%mod) if( b&1 ){
		ans= ans*a%mod;
	} return ans;
}
il ll inv(ll x){ return qpow(x,mod-2); }
il ll comp(ll n,ll m){
	if( m>n-m ) m = n-m;
	if( n<0 or m<0 ) return 0;
	ll up=1, down=1;
	rep(i,1,m){
		up = (up*(n-i+1))%mod;
		down = (down*i)%mod;
	}
	return up*inv(down)%mod;
}

int main(){
	int n,m,k;
	cin>>n>>m>>k;
	if( n>m+k ){ cout<<0<<endl; return 0; }
	cout<<((comp(n+m,n)-comp(n+m,n-k-1))%mod+mod)%mod<<endl;
	return 0;
}


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

const int nax = 805;
int n, k, a[nax][nax];
int sum[nax][nax];

int main() {
    cin >> n >> k;
    for(int i = 1; i <= n; i += 1)
        for(int j = 1; j <= n; j += 1)
            cin >> a[i][j];

    int L = 0, R = 1e9;
    while(L < R) {
        int M = (L + R + 1) / 2;
        for(int i = 1; i <= n; i += 1)
            for(int j = 1; j <= n; j += 1) {
                sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + (a[i][j] >= M ? 1 : -1);
        }
        int ok = 1;
        for(int i = k; i <= n; i += 1) {
            for(int j = k; j <= n; j += 1) {
                if(sum[i][j] - sum[i - k][j] - sum[i][j - k] + sum[i - k][j - k] <= 0)
                    ok = 0;
            }
        }
        if(ok) L = M;
        else R = M - 1;
    }
    cout << L << endl;
}

//设x是所求最小的中位数，若M <= x, 所有的k * k的sum矩阵值为正;若M > x，所有的k * k的sum矩阵非正。所以二分求最大值即可

#include<bits/stdc++.h>

using namespace std;

#define  GODSPEED       ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0);cout<<fixed;cout<<setprecision(15);
#define  f              first
#define  s              second
#define  newl           cout<<"\n";
#define  pb             push_back
#define  mset(a,x)      memset(a,x,sizeof(a))
#define  debv(a)        for(auto it: a)cout<<it<<" ";newl;
#define  deb1(a)        cout<<a<<"\n";
#define  deb2(a,b)      cout<<a<<" "<<b<<"\n";
#define  deb3(a,b,c)    cout<<a<<" "<<b<<" "<<c<<"\n";
#define  deb4(a,b,c,d)  cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n";
#define  uniq(a)        a.resize(unique(a.begin(), a.end()) - a.begin());
#define  all(a)         a.begin(),a.end()

typedef  long long             ll;
typedef  long double           ld;
typedef  pair<ll, ll>           pll;
typedef  vector<ll>            vll;
typedef  vector<pll>           vpll;

const ll   N     =  2e3 + 5;
const ll   mod   =  1e9 + 7;
const ll   INF   =  1e18;
const int  INFi  =  0x7f7f7f7f;
const ll   LEVEL =  log2(N) + 1;

int n, k, a[805][805], b[805][805], x[805][805], y[805][805];

int f(int mid) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			b[i][j] = a[i][j] <= mid;
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			x[i][j] = y[i][j] = b[i][j];
			if (i) y[i][j] += y[i - 1][j];
			if (j) x[i][j] += x[i][j - 1];
		}
	}
	int ct = 0;
	for (int i = 0; i < k; i++) {
		for (int j = 0; j < k; j++) {
			ct += b[i][j];
		}
	}
	for (int i = 0; i <= n - k; i++) {
		if (i % 2 == 0) {
			if (i) {
				// for (int j = 0; j < k; j++)
				ct -= x[i - 1][k - 1];
				ct += x[i - 1 + k][k - 1];
			}
			// deb1(ct)
			if (ct >= (k * k + 1) / 2) return 1;
			for (int j = 0; j <= n - k - 1; j++) {
				// for (int ii = i; ii < i + k; ii++)
				ct -= y[i + k - 1][j];
				if (i) ct += y[i - 1][j];
				ct += y[i + k - 1][j + k];
				if (i) ct -= y[i - 1][j + k];
				// deb1(ct)
				if (ct >= (k * k + 1) / 2) return 1;
			}
		} else {
			// for (int j = n - 1; j > n - k - 1; j--)
			ct -= x[i - 1][n - 1];
			if (n - k) ct += x[i - 1][n - k - 1];
			ct += x[i + k - 1][n - 1];
			if (n - k) ct -= x[i + k - 1][n - k - 1];
			// deb1(ct)
			if (ct >= (k * k + 1) / 2) return 1;
			for (int j = n - 1; j >= k; j--) {
				// for (int ii = i; ii < i + k; ii++)
				ct -= y[i + k - 1][j];
				if (i) ct += y[i - 1][j];
				ct += y[i + k - 1][j - k];
				if (i) ct -= y[i - 1][j - k];
				// deb1(ct)
				if (ct >= (k * k + 1) / 2) return 1;
			}
		}
	}
	return 0;
}

void solve() {
	cin >> n >> k;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> a[i][j];
		}
	}
	ll l = 0, r = 1e9, ans;
	while (l <= r) {
		ll mid = (l + r) / 2;
		if (f(mid)) {
			ans = mid;
			r = mid - 1;
		} else l = mid + 1;
	}
	deb1(ans)
}

int main() {
	GODSPEED;
	int test = 1;
	// cin >> test;
	for (int i = 1; i <= test; i++) {
		solve();
	}
}


#ifdef __LOCAL
#define _GLIBCXX_DEBUG
#endif

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

#pragma region macros

using str=string;
using ll=long long;
using vl=vector<ll>;
using vc=vector<char>;
using pl=pair<ll,ll>;
using ml=map<ll,ll>;
using sl=set<ll>;
template<class T> using V=vector<T>;
template<class T,class U> using P=pair<T,U>;

#define FOR(n) for(ll i=0;i<n;i++)
#define rep(i,n) for(ll i=0;i<n;i++)
#define reps(i,n) for(ll i=1;i<n;i++)
#define REP(i,m,n) for(ll i=m;i<n;i++)
#define drep(i,n) for(ll i=n-1;i>=0;i--)
#define fore(n) for(auto&& i:n)
#define fors(n) for(auto&&[i,j]:n)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sor(v) sort(all(v))
#define rsor(v) sort(rall(v))
#define rev(v) reverse(all(v))
#define low(v,x) lower_bound(all(v),x)-v.begin()
#define up(v,x) upper_bound(all(v),x)-v.begin()
#define acc(v) accumulate(all(v),0LL)
#define ef(x) emplace_front(x)
#define eb(x) emplace_back(x)
#define pf() pop_front()
#define pb() pop_back()
#define mp(a,b) make_pair(a,b)
#define ceil(a,b) (a+b-1)/b
#define bit(n) (1LL<<n)

#define fi first
#define se second
#define fr front()
#define ba back()
#define be begin()
#define en end()
#define br break
#define cn continue
#define wh while
#define el else if
#define re return

const ll inf=1LL<<60;
const ll mod=1000000007;
const ll MOD=998244353;
const double pi=3.1415926535;
const double eps=1e-10;
const ll dx[4]={1,0,-1,0};
const ll dy[4]={0,1,0,-1};

void input(){}
void inputs(){}
void output(){}
template<class T,class...U> void input(T& x,U&...y){cin>>x;input(y...);}
template<class...T> void in(T&...x){input(x...);}
template<class T> void inputs(const ll t,T& x){cin>>x[t];}
template<class T,class...U> void inputs(const ll t,T& x,U&...y){cin>>x[t];inputs(t,y...);}
template<class T,class...U> void inputs(T& x,U&...y){rep(t,size(x))inputs(t,x,y...);}
template<class...T> void ins(T&...x){inputs(x...);}
template<class T,class...U> void output(T x,U...y){cout<<x<<"\n";output(y...);}
template<class...T> void out(T...x){output(x...);}
template<class...T> void fin(T...x){out(x...);exit(0);}
template<class T> istream&operator>>(istream& i,V<T>& v){for(T& x:v)i>>x;re i;}
template<class T,class U> istream&operator>>(istream& i,P<T,U>& p){re i>>p.fi>>p.se;}
template<class T> ostream&operator<<(ostream& o,V<T>& v){for(T& x:v)o<<x<<" ";re o;}
template<class T,class U> ostream&operator<<(ostream& o,P<T,U>& p){re o<<p.fi<<" "<<p.se;}

void yn(bool b){fin((b?"Yes":"No"));}
void YN(bool b){fin((b?"YES":"NO"));}
void pos(bool b){fin((b?"POSSIBLE":"IMPOSSIBLE"));}
template<class T,class U> void chmax(T& a,U b){if(a<b)a=b;}
template<class T,class U> void chmin(T& a,U b){if(a>b)a=b;}
template<class T,class U> auto max(T a,U b){re a>b?a:b;}
template<class T,class U> auto min(T a,U b){re a<b?a:b;}
ll qp(ll x,ll n){ll r=1;wh(n>0){if(n&1)r*=x;x*=x;n>>=1;}re r;}
ll mdp(ll x,ll n){ll r=1;wh(n>0){if(n&1)r=r*x%mod;x=x*x%mod;n>>=1;}re r;}
ll MDp(ll x,ll n){ll r=1;wh(n>0){if(n&1)r=r*x%MOD;x=x*x%MOD;n>>=1;}re r;}

struct UnionFind{
    vl par,sz;
    UnionFind(ll n):par(n),sz(n,1){rep(i,n) par[i]=i;}
    ll rot(ll x){
        if(par[x]==x) re x;
        re par[x]=rot(par[x]);
    }
    void uni(ll x,ll y){
        x=rot(x),y=rot(y);
        if(x==y) re;
        if(sz[x]<sz[y]) swap(x,y);
        sz[x]+=sz[y];
        par[y]=x;
    }
    bool eql(ll x,ll y){re rot(x)==rot(y);}
    ll siz(ll x){re sz[rot(x)];}
};

struct IOSetup{
    IOSetup(){
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout<<fixed<<setprecision(10);
    }
}iosetup;

#pragma endregion

signed main(){
    ll h,w; in(h,w);
    V<str> s(h); in(s);

    ll ans=1;
    V<vc> v(h+w-1);
    rep(i,h) rep(j,w) v[i+j].eb(s[i][j]);
    rep(j,h+w-1){
        bool a=0,b=0;
        fore(v[j]){
            if(i=='R') a=1;
            if(i=='B') b=1;
        }
        if(a&&b) fin(0);
        if(!a&&!b){
            ans*=2;
            ans%=MOD;
        }
    }
    out(ans);
}

#include <bits/stdc++.h>
#define int long long int
#define all(x) x.begin(), x.end()
#define send ios_base::sync_with_stdio(false);
#define help cin.tie(NULL)
#define inf (int)(1e17+1)
#define mod (int)(998244353)
#define N (int)(2e5+5)
#define fi first
#define se second
#define endl "\n"
#define double long double
#define eps (double)(1e-9)
#define sa cout<<"sa"<<endl
using namespace std;

namespace math{
	vector <int> fac(N,1);
	int exp(int a,int b){
		if(!b) return 1;
		if(b&1) return a*exp(a,b-1)%mod;
		int t=exp(a,b/2);
		return t*t%mod;
	}
	void fc(){for(int i=2;i<N;i++)fac[i]=i*fac[i-1]%mod;}
	int com(int n,int r){return (fac[n]*exp(fac[r]*fac[n-r]%mod,mod-2))%mod;}
	int per(int n,int r){return (fac[n]*exp(fac[n-r],mod-2))%mod;}
	vector <int> getpr(int x){
		vector <int> res;if(x!=1) res.push_back(x);
		for(int i=2;i*i<=x;i++){
			if(i*i==x) res.push_back(i);
			else if(x%i==0) res.push_back(i),res.push_back(x/i);;
	}return res;}
}using namespace math;

void solve(){
	int n,m;
	cin>>n>>m;
	vector <string> v(n);
	for(int i=0;i<n;i++) cin>>v[i];
	int ans=0;
	vector <int> path(m+n);
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(v[i][j]=='B'&&path[i+j]==1) {cout<<0;return;}
			if(v[i][j]=='R'&&path[i+j]==2) {cout<<0;return;}
			if(v[i][j]=='B') path[i+j]=2;
			else if(v[i][j]=='R') path[i+j]=1;
		}
	}
	for(int i=0;i<n+m-1;i++) ans+=!path[i];
	cout<<exp(2,ans);
}

int32_t main(){
    send help;
    #ifdef LOCAL
  	freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
	#endif
	int t=1;
	//cin>>t;
	while(t--) solve();
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define rg register
#define In inline
#define ll long long
#define rep(i,l,r) for(rg int i = (l);i <= (r);i++)
#define dwn(i,r,l) for(rg int i = (r);i >= (l);i--)
#define mp make_pair
#define fi first
#define se second

const int N = 2e5;

namespace IO{
	In ll read(){
		ll s = 0,ww = 1;
		char ch = getchar();
		while(ch < '0' || ch > '9'){if(ch == '-')ww = -1;ch = getchar();}
		while('0' <= ch && ch <= '9'){s = 10 * s + ch - '0';ch = getchar();}
		return s * ww;
	}
	In void write(ll x){
		if(x < 0)putchar('-'),x = -x;
		if(x > 9)write(x / 10);
		putchar('0' + x % 10);
	}
};
using namespace IO;

ll n,v[2*N+5];
priority_queue<ll,vector<ll>,greater<ll> > pq;

void solve(){
	rep(i,1,n){
		pq.push(v[n+i]);
		pq.push(v[n-i+1]);
		pq.pop();
	}
	ll ans = 0;
	while(!pq.empty()){
		ans += pq.top();
		pq.pop();
	}
	write(ans),putchar('\n');
}

int main(){
//	freopen("B.in","r",stdin);
//	freopen("B.out","w",stdout);
// USE LONG LONG
	n = read();
	rep(i,1,2 * n)v[i] = read();
	solve();
	return 0;
}
// T: Think
// E: Enough array size
// M: Memory limit
// P: Precision
// B: Boundary
// O: Overflow
// T: Time limit
// M: Mod
// F: File

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(i=0;i<n;i++)
#define Rep(i,x,n) for(i=x;i<=n;i++)
#define foreach(c,itr) for(__typeof(c)::iterator itr=c.begin();itr!=c.end();itr++)
#define all(v) v.begin(),v.end()
#define p_b push_back
#define fr first
#define sc second
#define m_p make_pair
#define zero(a) memset(a,0,sizeof(a))
#define set_P setprecision
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f;
ll gcd(ll x,ll y) {return x%y==0 ? y:gcd(y,x%y);}
int i,j;

int n,k;
int a[300005];
int num[300005];

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

    cin>>n>>k;
	Rep(i,1,n){
		cin>>a[i];
		num[a[i]]++;
	}
	ll ans=0;
	for(i=1;i<=k;i++){
		int pos=0;
		while(num[pos]>0){
			num[pos]--;
			pos++;
		}
		ans+=pos;
	}
	cout<<ans<<endl;
	return 0;
}