#include <stdio.h>
#include <math.h>
#define MAXINPUT 100

int main(void){
	int h[MAXINPUT],w[MAXINPUT];
	int ansh[MAXINPUT]={},answ[MAXINPUT]={};
	int i=0,j=0;
	int ans=0;
	int num=0;
	int k;
	while(i<MAXINPUT){
		scanf("%d %d",&h[i],&w[i]);
		if(h[i]==0 && w[i]==0)break;
		i++;
		num++;
	}

	for(i=0;i<num;i++,ans=0){
		ans=h[i]*h[i]+w[i]*w[i];
		for(;(ansh[i]==0||answ[i]==0);){
			for(j=1;k==0;j++){
				double temp=ans-(j*j);
				double tempa=0;
				if(temp<=0){
					k=1;
					break;
				}
				temp=sqrt(temp);
				temp=modf(temp,&tempa);
				if(temp==0 && tempa<=150 && j<tempa){
					if(tempa*tempa+j*j==h[i]*h[i]+w[i]*w[i] && j<=h[i]){
						continue;
					}
					ansh[i]=j;
					answ[i]=(int)tempa;
					printf("%d %d\n",ansh[i],answ[i]);
					break;
				}
			}
		ans++;
		k=0;
		}
	}

	return 0;
}

#include "bits/stdc++.h"

using namespace std;

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

#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#ifdef Debug
#define dump(x)  cerr << #x << " = " << (x) << endl
#else
#define dump(x)
#endif

int main(){
	int h,w;
	while(cin>>h>>w){
		if(h==0 && w==0)break;
		int a=9999,b=9999;

		for(int i=1;i<=150;i++){
			for(int j=i+1;j<=150;j++){
				if(h*h+w*w<i*i+j*j || (h*h+w*w==i*i+j*j && h<i )){
					if(i*i+j*j<a*a+b*b || ( i*i+j*j==a*a+b*b && i<a )){
						a=i;b=j;
					}
				}
			}
		}
		cout<<a<<" "<<b<<endl;
	}
	return 0;
}


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

bool IsPrime(long long n){
  if(n == 1) return false;
  for(long long i = 2; i * i < n; i++){
    if(n%i == 0) return false;
  }
  return true;
}

vector<pair<long long, long long>> PrimeFactorize(long long n){
  vector<pair<long long, long long>> res;

  long long a = 0;

  for(long long i = 2; i * i <= n; i++){
    a = 0;
    if(n%i == 0){
      if(IsPrime(i)){
        while(n % i == 0){
          n /= i;
          a++;
        }
        res.push_back(make_pair(i, a));
      }
    }
  }

  if(n!= 1) res.push_back(make_pair(n, 1));
  return res;
}

int NumOfDivisor(long long n){
  vector<pair<long long, long long>> res;
  res = PrimeFactorize(n);

  int num = 1;
  for(int i = 0; i < res.size(); i++){
    num *= (1 + res[i].second);
  }

  return num;

}

int main(){
  const int mod = 1e9 + 7;
  int n;
  const int max_n = 1e3;
  cin >> n;

  long long ans = 1;
  long long ex[max_n + 2]  = {};

  for(int i = 1; i <= n; i++){
    vector<pair<long long, long long>> res;
    res = PrimeFactorize(i);
    for(int j = 0; j < res.size(); j++){
      ex[res[j].first - 1] += res[j].second;
    }
  }

  for(int i = 0; i < n; i++){
    ans *= (ex[i] + 1);
    ans %= mod;
  }

  cout << ans << endl;

  return 0;
}

/*
ID: anonymo14
TASK: wormhole
LANG: C++
*/
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pair<int,int>> vpii;

#define F first
#define S second
#define PU push
#define PUF push_front
#define PUB push_back
#define PO pop
#define POF pop_front
#define POB pop_back

#define REP(i,a,b) for(int i=a; i<=b; i++)

#define MOD 1000000007

void solve(int test_case) {//211
	ll n;
	cin>>n;
	vector<bool> prime(n+1,1);
	REP(i,2,n) {
		if(prime[i])for(int j=i+i;j<=n;j+=i)prime[j]=0;
	}
	ll ans = 1;
	for(ll i=2;i<=n;i++) {
		if(prime[i]) {
			ll now = i,cnt=0;
			while(now<=n){cnt=cnt+(n/now);now*=i;}
			ans = (ans*(cnt+1))%1000000007;
		}
	}
	//if(n!=1)ans = (ans*2)%1000000007;
	cout<<ans;
}//1, 2, 4, 8, 12, 30, 60, 96

int main() {

	////// FILE BASED IO////
	//freopen("wormhole.in", "r", stdin);
	//freopen("wormhole.out", "w", stdout);
	///////////////

	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//cin>>t;
	REP(i,1,t) {
		solve(i);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
//#define MOD 1000000007
#define MOD 998244353
#define INF 1145141919810893364
//#define INF 810114514
#define PI 3.141592653589
typedef long long ll;
typedef pair<ll,ll> PP;
#define int ll
#define setdouble setprecision
#define REP(i,n) for(int i=0;i<(n);++i)
#define OREP(i,n) for(int i=1;i<=(n);++i)
#define RREP(i,n) for(int i=(n)-1;i>=0;--i)
#define GOODBYE do { cout << "0" << endl; return 0; } while (false)
#define MM <<" "<<
#define Endl endl
#define debug false
#define debug2 false

bool comp(pair<pair<int,int>,string> G,pair<pair<int,int>,string> H){
    int Ga=G.first.first;
    int Gb=G.first.second;
    int Ha=H.first.first;
    int Hb=H.first.second;

    if(Ga-Gb>=0 && Ha-Hb<0)return true;
    if(Ga-Gb<0 && Ha-Hb>=0)return false;

    if(Ga-Gb>=0 && Ha-Hb>=0){
        return Gb<Hb;
    }else{
        return Ga>Ha;
    }
}

signed main(void){
    int N;
    string S;
    vector<pair<pair<int,int>,string>> V;

    cin >> N;
    REP(i,N){
        cin >> S;
        int a=0,b=0;
        REP(j,S.size()){
            if(S[j]=='('){
                a++;
            }else{
                b++;
            }
        }
        V.push_back({{a,b},S});
    }

    sort(V.begin(),V.end(),comp);

    //REP(i,N){cout << V[i].second << endl;}

    int now=0;
    bool ok = true;
    REP(i,N){
        string S = V[i].second;
        REP(j,S.size()){
            if(S[j]=='('){
                now++;
            }else{
                now--;
            }
            if(now<0)ok=false;
        }
    }
    if(now!=0)ok = false;

    cout << (ok?"Yes":"No") << endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = 998244353;
struct node{
    int l,r;
};
const int N = 1e6+7;
string s[N];
int n;
vector<node>a;
vector<node>b;
bool cmp(node x,node y)
{
    return x.r<y.r;
}
bool cmp1(node x,node y){
    return x.l<y.l;
}
int main()
{
    cin >> n;
    for(int i = 1;i<=n;++i)
    {
        cin >>s[i];
        int len  =s[i].length();
        int l = 0,r = 0;
        for(int j = 0;j<len;++j)
        {
            if(s[i][j]=='(')++l;
            else if(l)--l;
            else ++r;
        }
        if(l>=r) a.push_back({l,r});
        else b.push_back({l,r});
    }
    sort(a.begin(),a.end(),cmp);
    sort(b.begin(),b.end(),cmp1);
    int sum = 0,ok = 0;
    for(int i = 0;i<a.size();++i)
    {
        sum-=a[i].r;
        if(sum<0)
        {
            ok = 1;
            break;
        }
        sum+=a[i].l;
    }
    int sum1 = 0;
    for(int i = 0;i<b.size();++i)
    {
        sum1-=b[i].l;
        if(sum1<0)
        {

            ok = 1;
            break;
        }
        sum1+=b[i].r;
    }
    if(ok)
        puts("No");
    else
    {
        if(sum==sum1)
            puts("Yes");
        else
            puts("No");
    }

    return 0;
}


#include <iostream>
using namespace std;

long long int get_digit(long long int l){
    long long int digit = 0;

    while(l != 0){
        l /= 10;
        digit++;
    }

    return digit;
}

int main(void){
    // Your code here!
    long long int n;
    cin >> n;

    long long int j;
    long long int x,y,z;
    long long int ans = 20000000000000;

    for(long long int i=1; i*i <= n;i++){//iは０からエラー
        if(n%i == 0){
            j = n/i;
            x = get_digit(i);
            y = get_digit(j);
            z = max(x,y);
            if(z < ans){
                ans = z;
            }
        }
    }

    cout << ans << endl;
}

// -*- mode:c++; coding:utf-8; indent-tabs-mode:nil; -*-
// ALDS1_4-A: Linear Search

#include <cstdio>

static int linear_search(const int as[], const int target) {
  int i = 0;
  while (as[i] != target) {
    ++i;
  }
  return i;
}

int main(void) {
  int n;
  std::scanf("%d", &n);
  int ss[n+1]; // n+1(sentinel)
  for (auto i = 0; i < n; ++i) {
    std::scanf("%d", &ss[i]);
  }

  int q;
  std::scanf("%d", &q);
  int c = 0;
  for (auto i = 0; i < q; ++i) {
    int t;
    std::scanf("%d", &t);
    ss[n] = t; // sentinel
    auto j = linear_search(ss, t);
    if (j != n) ++c;
  }

  std::printf("%d\n", c);

  return 0;
}
// end


#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
using namespace std;
typedef unsigned long long ll;
typedef unsigned int ui;
const ll mod = (ll)(1e+9) + 7;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-12;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
ll bit[1 << 17][2]; int n;
void add(int id,int a, ll w) {
	for (int i = a; i <= n + 1; i += i & -i)bit[i][id] += w;
}
ll sum(int id,int a) {
	ll ret = 0;
	for (int i = a; i > 0; i -= i & -i)ret += bit[i][id];
	return ret;
}
int a[1 << 17][3];
P b[1 << 17];
int main() {
	cin >> n;
	rep(i, 3) {
		rep(j, n) {
			cin >> a[j][i]; a[j][i]--;
		}
	}
	bool f = true;
	rep(i, n) {
		int key = a[i][0]/3;
		rep1(j, 2) {
			if (key != a[i][j] / 3)f = false;
		}
		if (a[i][1] == a[i][0] + 1 && a[i][2] == a[i][0] + 2) {
			b[key] = { i+1,0 };
		}
		else if (a[i][1] == a[i][0] - 1 && a[i][2] == a[i][0] - 2) {
			b[key] = { i+1,1 };
		}
		else f = false;
		if (i % 2 != key % 2)f = false;
		//cout << key << endl;
	}
	if (!f) {
		cout << "No" << endl;
	}
	else {
		ll cnt[2] = {};
		rep(i, n) {
			int k = i % 2;
			cnt[k ^ 1] += b[i].second;
			cnt[k] += (b[i].first-1)/2 - sum(k,b[i].first);
			add(k,b[i].first,1);
			//cout << cnt << endl;
		}
		//cout << cnt[0] << " " << cnt[1] << endl;
		if (cnt[0]% 2==0&&cnt[1]%2==0)cout << "Yes" << endl;
		else cout << "No" << endl;
		//cout << cnt << endl;
	}
	return 0;
}

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

double ev[305][305][305], p[305][305][305];

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

    vector<int> cnt(4,0);

    for(int i=0; i<n; i++)
    {
        int x;
        cin>>x;

        cnt[x]++;
    }

    p[cnt[1]][cnt[2]][cnt[3]] = 1;

    for(int c=n; c >= 0; c--)
    {
        for(int b=n; b >= 0; b--)
        {
            for(int a=n; a >= 0; a--)
            {
                if(a==0 && b==0 && c==0)
                    continue;

                if(a+b+c > n)
                    continue;

                double p_waste = (double)(n - (a+b+c))/n;
                double ev_waste = (double)p_waste/(1 - p_waste);

                //debug() << imie(a) imie(b) imie(c) imie(ev_waste);

                ev[a][b][c] += ev_waste*p[a][b][c];

                if(a != 0)
                {
                    double p_go = (double)a/(a+b+c);
                    p[a-1][b][c] += p[a][b][c]*p_go;
                    ev[a-1][b][c] += ev[a][b][c]*p_go;
                }

                if(b != 0)
                {
                    double p_go = (double)b/(a+b+c);
                    p[a+1][b-1][c] += p[a][b][c]*p_go;
                    ev[a+1][b-1][c] += ev[a][b][c]*p_go;
                }

                if(c != 0)
                {
                    double p_go = (double)c/(a+b+c);
                    p[a][b+1][c-1] += p[a][b][c]*p_go;
                    ev[a][b+1][c-1] += ev[a][b][c]*p_go;
                }
            }
        }
    }

    cout<<fixed<<setprecision(10)<<ev[0][0][0] + cnt[1] + 2*cnt[2] + 3*cnt[3]<<endl;
}


#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

#define rep(i, n) for(ll i = 0; i < n; i++)
#define REP(i, n) for(ll i = 1; i < n + 1; i++)
#define PI 3.14159265359
#define EPS 0.0000000001
#define MOD 1000000007
//cout << std::fixed << std::setprecision(15) << y << endl;

int main(){
    string S;
    cin >> S;

    sort(S.begin(), S.end());

    char ans = 'a';
    rep(i, S.size()){
        if(S[i] == ans){
            ans++;
        }else if(S[i] > ans){
            break;
        }
    }

    if(ans > 'z'){
        cout << "None" << endl;
    }else{
        cout << ans << endl;
    }

    return 0;
}

#include "bits/stdc++.h"

using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define int ll
typedef pair<int,int> pii;
#define fi first
#define se second
#define Sort(a) sort(a.begin(),a.end())
#define rep(i,n) for(int i = 0;i < (n) ; i++)
#define REP(i,n) for(int i = 0;i < (n) ; i++)
#define MP(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define INF LLONG_MAX/4
#define INTINF INT_MAX/2
#define all(x) (x).begin(),(x).end()
#define debug(x) cerr<<#x<<": "<<x<<endl
#define debug_vec(v) cerr<<#v<<":";rep(i,v.size())cerr<<" "<<v[i];cerr<<endl
template<class T> inline bool chmax(T &a, T b){if(a < b){a = b;return true;}return false;}
template<class T> inline bool chmin(T &a, T b){if(a > b){a = b;return true;}return false;}
/*----------------------------------------------------------------*/
const int MOD = 998244353;
// const int MOD = 1000000007;

/*----------------------------------------------------------------*/
signed main(){

	string s;
	cin >> s;
	int cnt[26] = {};
	for(auto c:s)cnt[c-'a']++;

	rep(i,26){
		if(cnt[i] == 0){
			cout << (char)('a'+i) << endl;
			return 0;
		}
	}
	cout << "None" << endl;

	return 0;
}
/*----------------------------------------------------------------*/

// g++ -std=gnu++17 code1.cpp
// sudo pip3 install --upgrade online-judge-tools
// rm -r -f test;oj dl https://code-festival-2018-quala.contest.atcoder.jp/tasks/code_festival_2018_quala_c
// rm -r -f test;oj dl https://abc071.contest.atcoder.jp/tasks/abc071_b


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

#pragma GCC optimize("O2,no-stack-protector,unroll-loops,fast-math")

#define F first
#define S second
#define pb push_back
#define SZ(x) (ll)(x.size())
#define all(x) x.begin(),x.end()
#define MP make_pair

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll maxn=3e2+10, maxm=5e4+10, lg=21, mod=998244353, inf=1e18;

string s;
ll k,n,cnt,a[maxn],d[maxn],dp[maxn][maxn][maxn];

int main(){
	ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);

	cin>>s>>k;
	for(char c:s) (c=='0' ? cnt++:a[n++]=cnt);
	k=min(k,n);
	for(int i=0;i<n;i++) d[a[i]]++;
	dp[cnt+1][0][0]=1;
	for(int i=cnt+1;i;i--)for(int j=0;j<=k;j++)for(int k=0;k<=n;k++)if(dp[i][j][k])for(int t=0;t<=d[i-1]+k;t++) (dp[i-1][j+max(0ll,d[i-1]-t)][d[i-1]+k-t]+=dp[i][j][k])%=mod;
	ll ans=0;
	for(int i=0;i<=k;i++) ans+=dp[0][i][0];
	cout<<ans%mod;

	return 0;
}


#include <stdio.h>

int main(){
	int a, b, c, d;
	int K;
	scanf("%d %d %d %d %d", &a, &b, &c, &d, &K);
	int diff1 = c - a;
	int diff2 = d - b;
	int min = (diff1 * 60) + diff2;
	printf("%d", min - K);

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define mod 998244353
#define floop(i,a,b) for (int i=(a);i<(b);i++)
#define rloop(i,a,b) for (int i=(a);i>=(b);i--)
#define floopeq(i,a,b) for (int i=(a);i<(b);i++)
#define pa pair<int,int>
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll power(ll x, ll y, ll p)
{
    ll res = 1;x = x % p;if (x == 0)return 0;while (y > 0)
    {if (y & 1) res = (res*x) % p;  y = y>>1; x = (x*x) % p;  }  return res;
}
void dfs(int i,int j,int n,int m,vector<vector<char>>a,vector<vector<bool>>&vis)
{
    vis[i][j]=1;
    if(i<(n-1) && vis[i+1][j]==0 && (a[i+1][j]!='#'))
    {
        dfs(i+1,j,n,m,a,vis);
    }
    if(i>0 && vis[i-1][j]==0 &&(a[i-1][j]!='#'))
    {
        dfs(i-1,j,n,m,a,vis);
    }
    if(j<(m-1) && vis[i][j+1]==0 && (a[i][j+1]!='#'))
    {
        dfs(i,j+1,n,m,a,vis);
    }
    if(j>0 && vis[i][j-1]==0 && ((a[i][j-1]!='#')))
    {
        dfs(i,j-1,n,m,a,vis);
    }
}
void solve()
{
	int h1,m1,h2,m2,k;
	cin>>h1>>m1>>h2>>m2>>k;
	int al = (60*(h2-h1)) + (m2-m1);
	cout<<al-k<<endl;
}
int main()
{

    int t;
    t=1;
    while(t--){
    	solve();
    }
    return 0;

}


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

int main() {
	ios::sync_with_stdio(0), cin.tie(0);
	int na, nb, nt;
	cin >> na >> nb >> nt;
	vector<int> a(na), b(nb);
	for (int i = 0; i < na; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < nb; i++) {
		cin >> b[i];
	}
	int ans = *min_element(a.begin(), a.end()) + *min_element(b.begin(), b.end());
	for (int i = 0; i < nt; i++) {
		int x, y, c;
		cin >> x >> y >> c;
		x--; y--;
		ans = min(ans, a[x] + b[y] - c);
	}
	cout << ans << '\n';
}

#include <bits/stdc++.h>
typedef long long int LL;
typedef unsigned long long int ULL;
using namespace std;

// 插入此處

LL a[100005];

int main() {
    LL n, m, v, p;
    cin >> n >> m >> v >> p;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sort(a, a + n, [](LL x, LL y) { return x > y; });
    // for (int i = 0; i < n; i++) {
    //     printf("%lld ", a[i]);
    // }
    // puts("");
    LL ans;
    LL agg = 0;
    for (ans = p; ans < n; ans++) {
        // 前後都還夠塞
        LL les = n - ans;
        LL re = v - (p - 1) - les;
        agg += a[ans - 1];
        if (re <= 0) {
            if (a[p - 1] <= a[ans] + m) {
                continue;
            } else {
                break;
            }
        } else {
            // 要開始塞中間了
            LL space = (ans - p + 1) * (a[ans] + m) - agg;
            LL vote = re * m;
            // printf("ans = %lld, re = %lld, space = %lld, vote = %lld\n", ans, re, space, vote);
            if (a[p - 1] <= a[ans] + m && space >= vote) {
                continue;
            } else {
                break;
            }
        }
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define fr(i,a,b) for(int i = a ; i <= b ; ++i)
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define pb push_back
#define fst first
#define snd second
using namespace __gnu_pbds;
using ordered_set =
 tree<int, null_type,less<int>,
 rb_tree_tag,tree_order_statistics_node_update>;
//find_by_order(k):returns iterator to kth element starting from 0
//order_of_key(k):returns count of elements strictly smaller than k
typedef long long ll;typedef pair<int, int> pii;
typedef vector<int> vi;typedef long double ld;
template<class T>
using min_heap = priority_queue<T,vector<T>,greater<T>>;
template<typename T> T gcd(T a, T b){return(b?__gcd(a,b):a);}
template<typename T> T lcm(T a, T b){return(a*(b/gcd(a,b)));}
template<class T> void re(T& x) { cin >> x; }
template<typename T>
void remdup(vector<T>& v) {
	sort(all(v)); v.erase(unique(all(v)), v.end());
}
template<typename T> void re(vector<T> &v) {trav(i,v) cin >> i;}
template<class H, class... T> void re(H& h, T&... t) { re(h); re(t...); }
void unsyncIO() { ios_base::sync_with_stdio(0); cin.tie(0); }
#ifdef np
#include "/home/o_o/MyCodes/pr.h"
#else
#define trace(...)
#endif

#define MOD 1000000007

void solve(){
	int cnt = 1 ;
	int n , x , t ;
	re(n,x,t);
	while(cnt*x<n) cnt++;
	cout << cnt * t << endl;
}

signed main() {
	unsyncIO();
	int tt =1 ;
	// cin >> tt;
	rep(i,0,tt) solve();

#ifdef np
	cout <<endl<<endl<< "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
long long s,ans;
int n,a[200003];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    	cin>>a[i];
    sort(a+1,a+1+n);
    for(int i=2;i<=n;i++)
	{
        s=(s+a[i-1])%1000000007;
        ans=(ans+s*a[i])%1000000007;
    }
    cout<<ans<<endl;
    return 0;
}


#include <stack>
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <numeric>

using namespace std;

using ll = long long;

#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--)

#define input(...) __VA_ARGS__; in(__VA_ARGS__)

#if __has_include("debugger.cpp")
  #include "debugger.cpp"
#else
void print() {
  std::cout << std::endl;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  cout << head;
  if (sizeof...(tail) > 0) cout << " ";
  print(std::forward<Tail>(tail)...);
}
# endif

void in() { }

template <class Head, class... Tail>
void in(Head&& head, Tail&&... tail) {
  cin >> head;
  in(std::forward<Tail>(tail)...);
}

int main() {
  string input(s);
  stack<char> p;
  REP(i, s.size()) {
    if (s[i] == 'T' && !p.empty() && p.top() == 'S') {
      p.pop();
    } else {
      p.push(s[i]);
    }
  }

  print(p.size());
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define In_The_Name_Of_Allah_The_Merciful ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define Alhamdulillah return 0;
#define all(v) v.begin(),v.end()
#define debug(n) cerr << "[" << #n << " = " << n << "]" << endl

void Suhaib_Sawalha (){

    string s , w;
    cin >> s >> w;
    cout << w + s;

}

int main(){
                                In_The_Name_Of_Allah_The_Merciful   /* بسم الله الرحمن الرحيم  */
#ifndef ONLINE_JUDGE
    freopen("SuhaibSawalha1.txt","r",stdin);
#endif
//int _;for(cin>>_;_;--_,cout<<'\n')
        Suhaib_Sawalha();
                                         Alhamdulillah                    /* الحمد لله */
}


#include <iostream>
#include <iomanip>

using namespace std;

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

int main() {
  int n, k;
  cin >> n >> k;
  int p[n];
  rep(i,n) cin >> p[i], p[i]++;

  int s = 0, m = 0;
  rep(i,n) {
    s = s + p[i] - (i >= k ? p[i-k] : 0);
    m = max(m, s);
  }

  cout << fixed << setprecision(1) << (double)m / 2 << endl;
  return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using ll = long long;

double expected_value(int n) {
  return (double)((1 + n) * n / 2) / (double)n;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<double> ps(n + 1);
  vector<double> comsum(n + 1, 0);
  int p;
  rep(i, n) {
    cin >> p;
    ps[i + 1] = expected_value(p);
    comsum[i + 1] = comsum[i] + ps[i + 1];
  }
  double mm = 0;
  for (int i = k; i <= n; i++) {
    mm = max(comsum[i] - comsum[i - k], mm);
  }
  printf("%.6f\n", mm);
  return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mk make_pair
#define MAX 1000001
ll prime[MAX];
#define mod 1000000007

ll rec(vector<vector<ll> >& dp,vector<ll>& arr,int n,int i,int k,int w)
{
  ll ans=0;
  if(i>=n)
    return 0;

  if(dp[i][k]!=-1)
    return dp[i][k];

  if(k!=0)
  ans=max(rec(dp,arr,n,i+1,w,w),arr[i]+rec(dp,arr,n,i+1,k-1,w));
  else
  ans=max(rec(dp,arr,n,i+1,k,w),rec(dp,arr,n,i+1,w,w));

  return dp[i][k]=ans;

}

ll t[400008];
void build(vector<ll>& a, int v, int tl, int tr) {
    if (tl == tr) {
        t[v] = a[tl];
    } else {
        int tm = (tl + tr) / 2;
        build(a, v*2, tl, tm);
        build(a, v*2+1, tm+1, tr);
        t[v] = max(t[v*2] ,t[v*2+1]);
    }
}

ll maxi(int v, int tl, int tr, int l, int r) {
    if (l > r)
        return 0;
    if (l == tl && r == tr) {
        return t[v];
    }
    int tm = (tl + tr) / 2;
    return max(maxi(v*2, tl, tm, l, min(r, tm)) ,maxi(v*2+1, tm+1, tr, max(l, tm+1), r));
}

void update(int v, int tl, int tr, int pos, ll new_val) {
    if (tl == tr) {
        t[v] = new_val;
    } else {
        int tm = (tl + tr) / 2;
        if (pos <= tm)
            update(v*2, tl, tm, pos, new_val);
        else
            update(v*2+1, tm+1, tr, pos, new_val);
        t[v] = max(t[v*2] ,t[v*2+1]);
    }
}

int main() {

int a;
cin>>a;
cout<<a+a*a+a*a*a;
return 0;
}

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

int main() {
    int a;
    cin >> a;
    int ans = a + a*a + a*a*a;
    cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <sstream>
#include <cstdio>

using namespace std;
int main()
{
    int n;
    string str;
    cin >> n;
    cin.get();
    for(int i=0;i<n;++i)
    {
        int loc;
        getline(cin, str);
        loc = str.find("Hoshino",0);
        while(loc!=string::npos)
        {
            str[loc+6] = 'a';
            loc = str.find("Hoshino",0);
        }
        cout << str << endl;
    }
}

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

main(){
	int n ;
	string s ;
	char c[] = {'H','o','s','h','i','n','o'} ;

	cin >> n ;
	for( int k=0 ; k<=n ; k++ ){
		getline( cin , s ) ;

		for( int i=0 ; i<s.size() ; i++ ){
			int cnt = 0 ;
			if( s[i] == 'H' ){
				for( int j=0 ; j<s.size() && j<7 ; j++ ){
					if( s[i+j] == c[j] ) cnt++ ;
					else break ;
				}
				if( cnt==7 ) s[i+6] = 'a' ;
			}
		}

		if( k ) cout << s << endl ;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pi 3.141592653589793238
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define MOD 1000000007

void solve()
{
	ll a,b,c;
	cin>>a>>b>>c;
	cout<<a+b+c-max({a,b,c});
    return;
}

int main()
{
        fast;
        ll q;
        q=1;
        // cin>>q;
        while(q--)
        	solve();

        return 0;
}

#include<queue>
#include<stack>
#include<math.h>
#include<cmath>
#include<bitset>
#include<stdio.h>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<iostream>
#include<utility>
#include<functional>
#include<deque>
#include<list>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int main(){
  int a[5],i,j;
  for(i=0;i<3;i++){
    cin >> a[i];
  }
  int ans=(1<<29);
  for(i=0;i<3;i++){
    for(j=i+1;j<3;j++){
      ans=min(ans,a[i]+a[j]);
    }
  }
  cout << ans << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
using st=string;
using ch=char;
typedef pair<ll,ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) FOR(i,0,n)
#define ROF(i,a,b) for(ll i=a;i>=b;i--)
#define per(i,a) ROF(i,a,0)
#define pb push_back
const ll MOD=1000000007;
const ll MOD2=998244353;
const ld PI=acos(-1);
const ll INF=1e18;
st abc="abcdefghijklmnopqrstuvwxyz";
st ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge{ll to,cost;};
struct edge2{ll from,to,cost;};

const ll MXN=100000;
vl G[MXN];
vvl d(2,vl(MXN,INF));

void dfs(ll x,ll v,ll z){
    d[x][v]=z;
    rep(i,G[v].size()){
        if(d[x][G[v][i]]==INF){
            dfs(x,G[v][i],z+1);
        }
    }
}

int main() {
    ll N,u,v;
    cin >> N >> u >> v;
    u--;v--;
    rep(i,N-1){
        ll a,b;
        cin >> a >> b;
        a--;b--;
        G[a].pb(b);
        G[b].pb(a);
    }
    dfs(0,u,0);
    dfs(1,v,0);
    ll D=0;
    rep(i,N){
        if(d[0][i]<d[1][i]){
            D=max(D,d[1][i]);
        }
    }
    cout << D-1 << endl;
}

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include <bitset>
#include <cmath>
#include <limits>
#include <iostream>
#include<set>
#include<tuple>
using namespace std;
#define INF 11000000000
#define MAX 100000
#define MOD 1000000007
typedef long long ll;
typedef pair<int,int> P;
typedef pair<pair<int,int>,int> p;
typedef pair< pair<int,int>, int> p;
#define bit(n,k) ((n>>k)&1) /*nのk bit目*/
#define rad_to_deg(rad) (((rad)/2/M_PI)*360)

vector<int> V[110000];
int color[110000];

//二部グラフならtrueを返す
bool dfs(int x,int c){
  color[x]=c;
  for(int i=0;i<V[x].size();i++){
    //隣接している頂点が同じ色ならfalse
    if(color[V[x][i]]==c) return false;
    //隣接している頂点がまだ塗られていないなら-cで塗る
    if(color[V[x][i]]==0 && !dfs(V[x][i],-c)) return false;
  }
  return true;
}

int main(){
    ll N,M;
    cin>>N>>M;
    for(int i=0;i<M;i++){
        int a,b;
        cin>>a>>b;
        a--; b--;
        V[a].push_back(b);
        V[b].push_back(a);
    }
    if(dfs(0,1)){
        ll count=0;
        for(int i=0;i<N;i++){
            if(color[i]==1) count++;
        }
        cout<<count*(N-count)-M<<endl;
    }else{
        cout<<N*(N-1)/2-M<<endl;
    }
}


#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0;i < (int)(n);i++)
using ll = long long;
const ll MOD=1000000007;
const long long INF = 1LL << 60;
const double pi=acos(-1.0);

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

ll N;

//dp
vector<vector<vector<double>>> table;

double dp(ll i,ll j,ll k){
  //cout<<i<<" "<<j<<" "<<k<<endl;
  if(i==0&&j==0&&k==0) return 0;
  if(table.at(i).at(j).at(k)>-1) return table.at(i).at(j).at(k);

  table.at(i).at(j).at(k)=0;
  if(i>0) table.at(i).at(j).at(k)+=dp(i-1,j,k)*i/N;
  if(j>0) table.at(i).at(j).at(k)+=dp(i+1,j-1,k)*j/N;
  if(k>0) table.at(i).at(j).at(k)+=dp(i,j+1,k-1)*k/N;
  table.at(i).at(j).at(k)+=1;

  return table.at(i).at(j).at(k)/=(double)(i+j+k)/N;
}

int main()
{
  cin>>N;
  vector<ll> vec(4,0); rep(i,N) {ll a; cin>>a; vec.at(a)++;}

  //dp
  table=vector<vector<vector<double>>>(N+1,vector<vector<double>>(N+1,vector<double>(N+1,-1)));

  cout<<fixed<<setprecision(15)<<dp(vec.at(1),vec.at(2),vec.at(3))<<endl;
  /*
  rep(i,2) {
    if(i!=0) cout<<" ";
    cout<<table.at(0).at(0).at(i);
  }
  cout<<fixed<<setprecision(15)<<endl;
  */

  return 0;
}

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

int D;
int d[22];
long long ans;

bool analyze(int ndig) {
	int tmp = D;
	for (int i = ndig; i >= 1; --i) {
		d[i] = tmp % 10; tmp /= 10;
	}
	return tmp == 0;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin >> D;
	for (int ndig = 2; ndig <= 20; ++ndig) {
		bool odd = ndig % 2;
		if (!analyze(ndig)) continue;

		long long cur = 1; // current answer
		// d[1], d[2], ..., d[ndig]
		bool lef = 0 /* -> */, rig = 0 /* <- */;
		for (int dig = 1; dig <= ndig / 2; ++dig) {
			bool nlef = 0, nrig = 0;
			bool LEF = 0, RIG = 0;
			// opposite: ndig + 1 - dig
			int npair = 0;
			for (int i = 0; i <= 9; ++i) {
				for (int j = 0; j <= 9; ++j) {
					// check for validity
					if (dig == 1) {
						if (j == 0) continue;
						if (i < j) continue;
					}

					// I - J ~ [d[dig] + 1, d[dig]]
					// J - I = d[n + 1 - dig] || 10 + J - I = d[n + 1 - dig]
					int I = i, J = j;
					if (lef) I += 10; if (rig) --J;

					if (I - j == d[dig]) nlef = 0;
					else if (I - j == d[dig] + 1) nlef = 1;
					else continue;

					if (J - i == d[ndig + 1 - dig]) nrig = 0;
					else if (10 + J - i == d[ndig + 1 - dig]) nrig = 1;
					else continue;

					// now valid
					++npair;
					LEF = nlef;
					RIG = nrig;
				}
			}
			cur *= npair;
			lef = LEF;
			rig = RIG;
		}
		if (!odd && lef != rig) continue;
		else if (odd) {
			int cnt = 0;
			for (int i = 0; i <= 9; ++i) {
				int I = i;
				if (lef) I += 10;
				if (rig) I--;
				if (I - i == d[(ndig + 1) / 2]) ++cnt;
			}
			cur *= cnt;
		}
		ans += cur;
	}

	cout << ans << endl;
}


#include <iostream>
#include <string>
#include <algorithm>
int main(void)
{
    std::string str1;
    std::string str2;
    std::cin >> str1 >> str2;
    int l1 = str1.size();
    int l2 = str2.size();
    int A[l1 + 1][l2 + 1] = {};
    for (int i = 0; i <= l1; i++)
    {
        for (int j = 0; j <= l2; j++)
        {
            if (i == 0)
            {
                A[i][j] = j;
            }
            else if (j == 0)
            {
                A[i][j] = i;
            }
            else
            {
                A[i][j] = std::min(A[i - 1][j] + 1, A[i][j - 1] + 1);
                if (str1[i - 1] == str2[j - 1])
                {
                    A[i][j] = std::min(A[i - 1][j - 1], A[i][j]);
                }
                else
                {
                    A[i][j] = std::min(A[i - 1][j - 1] + 1, A[i][j]);
                }
            }
            //std::cout << A[i][j] << " ";
        }
        //std::cout << "\n";
    }
    std::cout << A[l1][l2] << "\n";
    return 0;
}

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX/3

#define BV_SIZE 64ULL // 64bit
#define MAX_BV_NUM 100 // m <= 100*64 = 6400

unsigned long long Peq[MAX_BV_NUM][0x100];
int levenshteinDistance(string a,string b) {
  int m=a.size(),n=b.size();
  int num_of_bv = (m + BV_SIZE -1) / BV_SIZE;

  for (int i = 0; i < m; i++) {
    Peq[i / BV_SIZE][a[i]] |= 1ULL << ((unsigned long long)i % BV_SIZE);
  }

  // calc
  int score = m;
  unsigned long long Pv[MAX_BV_NUM], Mv[MAX_BV_NUM], Ph[MAX_BV_NUM], Mh[MAX_BV_NUM], Xv[MAX_BV_NUM], Xh[MAX_BV_NUM], Eq[MAX_BV_NUM], Xt[MAX_BV_NUM], Xa[MAX_BV_NUM];
  unsigned long long top = 1ULL << (((unsigned long long)m - 1ULL) % BV_SIZE);

  for(int i = 0; i < num_of_bv; i++) {
    Pv[i] = ~0ULL; Mv[i] = 0ULL;
  }
  Pv[num_of_bv - 1] &= ((top << 1ULL) - 1ULL);
  //cout << static_cast<bitset<64> >(Pv[num_of_bv - 1]) << endl;

  for (int j = 0; j < n; j++) {
    for(int i = 0; i < num_of_bv; i++){
      Eq[i] = Peq[i][b[j]];
      Xv[i] = Eq[i] | Mv[i];
      Xt[i] = Eq[i] & Pv[i];
    }

    unsigned long long carry = 0ULL;

    for(int i = 0; i < num_of_bv; i++){
      Xa[i] = Xt[i] + Pv[i] + carry;
      if(Xt[i] == ~0ULL && carry == 1ULL){
        carry = 1ULL;
      }else{
        unsigned long long cXt = Xt[i] + carry;
        carry = (((cXt & Pv[i]) | ((cXt | Pv[i]) & ( ~ (cXt + Pv[i]) ))) >> 63ULL) & 1ULL;
      }
    }

    for(int i = 0; i < num_of_bv; i++){
      Xh[i] = (Xa[i] ^ Pv[i]) | Eq[i];
    }

    for(int i = 0; i < num_of_bv; i++){
      Ph[i] = Mv[i] | ( ~ (Xh[i] | Pv[i]) );
      Mh[i] = Pv[i] & Xh[i];
    }

    if((Ph[num_of_bv - 1] & top) != 0ULL)score++;
    else if((Mh[num_of_bv - 1] & top) != 0ULL)score--;

    unsigned long long Ph_carry = 1ULL;
    unsigned long long Mh_carry = 0ULL;

    /*
    for(int i = num_of_bv - 1; i >= 0; i--){
      for(int k = 63; k >= 0; k--){
        if((Ph[i] >> k) & 1ULL)cout<<1;
        else cout<<0;
      }
    }
    cout<<endl;
    */

    for(int i = 0; i < num_of_bv; i++){
      unsigned long long nxt_Ph_carry = (Ph[i] >> 63ULL) & 1ULL;
      Ph[i] = (Ph[i] << 1ULL) | Ph_carry;
      Ph_carry = nxt_Ph_carry;
    }
    /*
    for(int i = num_of_bv -1; i >= 0; i--){
      for(int k = 63; k >= 0; k--){
        if((Ph[i] >> k) & 1ULL)cout<<1;
        else cout<<0;
      }
    }
    cout<<endl;
    cout<<endl;
    */

    for(int i = 0; i < num_of_bv; i++){
      unsigned long long nxt_Mh_carry = (Mh[i] >> 63ULL) & 1ULL;
      Pv[i] = ((Mh[i] << 1ULL) | Mh_carry ) | ( ~ (Xv[i] | Ph[i]) );
      Mh_carry = nxt_Mh_carry;
    }

    for(int i = 0; i < num_of_bv; i++){
      Mv[i] = Ph[i] & Xv[i];
    }
  }
  return score;
}

int main(){
	string a,b;
  cin>>a>>b;
  cout<<levenshteinDistance(a,b)<<endl;
	return 0;
}


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

long long solve(int n, const std::vector<long long> & a, const std::vector<long long> & b, const std::vector<long long> & c) {
    long long res { 0LL };

    for (int i = 0; i < n; ++i) {
        const long long m { b[i] };
        const long long an { std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), m)) };
        const long long cn { std::distance(c.begin(), std::upper_bound(c.begin(), c.end(), m)) };
        res += (an * (n - cn));
    }
    return res;
}

int main(int argc, const char * argv[]) {
    int n;
    std::cin >> n;

    std::vector<long long> a;
    for (int i = 0; i < n; ++i) {
        long long ai;
        std::cin >> ai;
        a.push_back(ai);
    }
    std::sort(a.begin(), a.end());

    std::vector<long long> b;
    for (int i = 0; i < n; ++i) {
        long long bi;
        std::cin >> bi;
        b.push_back(bi);
    }
    std::sort(b.begin(), b.end());

    std::vector<long long> c;
    for (int i = 0; i < n; ++i) {
        long long ci;
        std::cin >> ci;
        c.push_back(ci);
    }
    std::sort(c.begin(), c.end());

    std::cout << solve(n, a, b, c) << std::endl;

    return 0;
}

#include <algorithm>
#include <iostream> //標準入出力
#include <fstream>  //ファイルでの入出力
#include <vector>
#include <set>
#include <functional>
#include <string>
#include <utility> //pair
#include <cmath>  //sqrt
#include <iomanip> //setprecision
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
#define ALL(a)  (a).begin(),(a).end()
#define MAX 1000000
#define pair(a) pair<a,a>

ll binary_search(vector<ll>& s,ll key){
    auto isOK = [&](ll index){
        if(s[index] > key) return true;
        else return false;
    };

    ll ng=-1,ok=s.size();
    while(abs(ok-ng) > 1){
        ll mid = (ok+ng)/2;
        if(isOK(mid)) ok = mid;
        else ng = mid;
    };

    return ok;
}
void _main(){
    ll n; cin >> n;
    vector<ll> a(n),b(n),c(n);
    rep(i,n) cin >> a[i];
    rep(i,n) cin >> b[i];
    rep(i,n) cin >> c[i];
    sort(ALL(a));
    sort(ALL(b));
    sort(ALL(c));
    vector<ll> atob(n),btoc(n);
    ll out = 0;
    rep(i,n)  atob[i] = binary_search(b,a[i]);
    rep(i,n)  btoc[i] = binary_search(c,b[i]);
    rep(i,n){
        for (ll j = atob[i]; j < n; j++)
        {
            out += (n-btoc[j]);
        }
    }

    cout << out << endl;
    return;
}
int main() {
    cout << fixed << setprecision(10);
    _main();
    return 0;
}


#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include <bits/stdc++.h>
using namespace std;
template<class t> inline t read(t &x){
	char c=getchar();bool f=0;x=0;
	while(!isdigit(c)) f|=c=='-',c=getchar();
	while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	if(f) x=-x;return x;
}
template<class t,class ...A> inline void read(t &x,A &...a){
	read(x);read(a...);
}
template<class t> inline void write(t x){
	if(x<0) putchar('-'),write(-x);
	else{if(x>9) write(x/10);putchar('0'+x%10);}
}

const int N=1e5+5;
int n,m,p,a[N],b[N],ans=1e9;

signed main(){
	read(n,m,p);
	for(int i=1;i<=n;i++) read(a[i]);
	for(int i=1;i<=m;i++) read(b[i]);
	ans=*min_element(a+1,a+1+n)+*min_element(b+1,b+1+n);
	while(p--){
		int x,y,z;
		read(x,y,z);
		ans=min(ans,a[x]+b[y]-z);
	}
	write(ans);
}

#include<bits/stdc++.h>
#define N 10
using namespace std;
typedef long long ll;
int n,k,a[N];
set<ll> s;

int check(){
  int perm[N];
  for(int i=0;i<n;i++)perm[i]=i;
  do{
    ll num=0;
    for(int i=0;i<k;i++){
      if(!(a[perm[i]]/10))num=num*10+a[perm[i]];
      else num=num*100+a[perm[i]];
    }
    s.insert(num);
  }while(next_permutation(perm,perm+n));
  return s.size();
}

int main(){
  while(1){
    cin>>n>>k;
    if(!n&&!k)break;
    for(int i=0;i<n;i++)
      cin>>a[i];
    cout<<check()<<endl;
    s.clear();
  }
  return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef int_fast64_t ll;

template<int N, typename T, T M>
struct ModFact {
    T fact[N+1];
    constexpr ModFact() : fact() {
        fact[0] = 1;
        for (int i=0; i<N; ++i)
            fact[i+1] = (fact[i]*(i+1))%M;
    }
};

ModFact<100000, ll, MOD> fact;

ll N, M, ans = 1;
int tmp, cnt = 0;
constexpr ll th = 1LL<<45;
char S[200000];

int main() {
    scanf("%lld\n", &N);
    M = N << 1;
    fread_unlocked(S, 1, M, stdin);
	for(size_t i = 0; i < M;) {
		tmp = (S[i++]=='B') + cnt;
		if(tmp&1) cnt++;
		else {
            ans *= cnt;
            if(ans > th) ans %= MOD;
            if(--cnt < 0) break;
        }
	}
    ans = cnt == 0 ? ((ans % MOD) * fact.fact[N]) % MOD : 0;
    printf("%lld\n", ans);
    return 0;
}

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

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

    if(S[0] == 'W' || S[2 * N - 1] == 'W') {
        cout << 0 << endl;
        return 0;
    }

    vector<bool> f(2 * N);
    f[0] = true;
    for(int i=1; i<2*N; ++i) {
        if(S[i - 1] != S[i]) f[i] = f[i - 1];
        else f[i] = !f[i - 1];
    }

    if(count(begin(f), end(f), true) != N) {
        cout << 0 << endl;
        return 0;
    }

    long long MOD = 1000000007;
    long long res = 1;
    for(int i=1; i<=N; ++i) res = (res * i) % MOD;

    int cnt = 0;
    for(int i=0; i<2*N; ++i) {
        if(f[i]) {
            ++cnt;
        } else {
            res = (res * cnt) % MOD;
            --cnt;
        }
    }
    cout << res << endl;
}


#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <functional>
#include <set>
#include <sstream>
#include <cctype>
#include <stack>
#include <queue>
#include <cstring>
#include <map>

using namespace std;

int main(){

	int n;
	while(cin>>n,n){

		map<long double,set<string> > ans;
		for(int i=0;i<n;i++){
			string name;
			long double p,a,b,c,d,e,f,s,m;
			cin>>name>>p>>a>>b>>c>>d>>e>>f>>s>>m;
			ans[((f*s*m)-p)/(a+b+c+m*(d+e))].insert(name);
		}

		for(auto it=ans.rbegin();it!=ans.rend();it++){
			set<string> ss =it->second;
			for(auto it2=ss.begin();it2!=ss.end();it2++) cout<<(*it2)<<endl;

		}
		cout<<"#"<<endl;
	}

	return 0;
}


#include <stdio.h>
#include <algorithm>
int n;
int a[100010];
int b[100010];
int c[100010];
long long psum_b[100010];
long long ans;
long long find_bc(int st,int ed,int x){
	int mid = (st+ed)/2;
	if(ed-st<=1){
		if(x<c[st]) return n-st;
		if(x>=c[ed]) return n-ed-1;
		return n-ed;
	}
	if(c[mid]<=x) return find_bc(mid,ed,x);
	return find_bc(st,mid,x);
}
long long find_ab(int st,int ed,int x){
	int mid = (st+ed)/2;
	if(ed-st<=1){
		if(x<b[st]) return psum_b[st];
		if(x>=b[ed]) return psum_b[ed+1];
		return psum_b[ed];
	}
	if(b[mid]<=x) return find_ab(mid,ed,x);
	return find_ab(st,mid,x);
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	std::sort(a,a+n);
	for(int i=0;i<n;i++){
		scanf("%d",&b[i]);
	}
	std::sort(b,b+n);
	for(int i=0;i<n;i++){
		scanf("%d",&c[i]);
	}
	std::sort(c,c+n);
	for(int i=n-1;i>=0;i--){
		psum_b[i]=psum_b[i+1]+find_bc(0,n-1,b[i]);
	}
	for(int i=0;i<n;i++){
		ans+=find_ab(0,n-1,a[i]);
	}
	printf("%lld\n",ans);
}

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

int N;
int A[3][100001];
LL dp[3][100001] = {};

int main() {
    scanf("%d", &N);
    rep(j, 3) { rep(i, N) scanf("%d", &A[j][i]); A[j][N] = 1000000001; }
    rep(j, 3) sort(A[j], A[j] + N);
    rep(i, N + 1) dp[0][i] = i;
    rep(j, 2) {
        int p = 0;
        rep(i, N) {
            while (A[j][p] < A[j + 1][i]) p++;
            dp[j + 1][i + 1] += dp[j + 1][i] + dp[j][p];
        }
    }
    printf("%lld\n", dp[2][N]);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll INF = 1e18;

int main(){
    int N;
    cin >> N;
    vector<ll> a(N);
    ll sum =0;
    for(int i=0;i<N;i++){
        cin >> a[i];
        sum += a[i];
    }
    ll m = INF;
    for(int i=0;i<N;i++){
        m = min(m,abs(sum- N*a[i]));
    }
    for(int i=0;i<N;i++){
        if(abs(sum-N*a[i]) == m){
            cout << i << endl;
            break;
        }
    }
    return 0;

}

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <math.h>
#include <map>

using namespace std;

int main(){
	int n,sum=0,mi_idx;
	double avg,mi=1e9;
	cin >> n;
	vector<int> d(n,0);

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

	avg=(double)sum/(double)n;

	for(int i=n-1;i>=0;i--){
		if(mi>=(avg-d[i])*(avg-d[i])){
			mi_idx=i;
		}
		mi=min(mi,(avg-d[i])*(avg-d[i]));
	}

	cout << mi_idx << endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

int w,h,m[21][21],sx,sy,gx,gy,dx[]={1,0,-1,0},dy[]={0,1,0,-1};

int f(int w,int h,int m[][21],int x,int y,int t){
	if(t==10)return 99;
	int res=99;
	rep(i,4){
		int cx=x,cy=y,d=0;
		while(1){
			cx+=dx[i];cy+=dy[i];
			if(cx<0||cx>=w||cy<0||cy>=h)break;
			if(m[cy][cx]==3){res=min(res,t+1);break;}
			if(m[cy][cx]==1){
				if(!d)break;
				int m2[21][21];
				rep(j,h)rep(k,w)m2[j][k]=m[j][k];
				m2[cy][cx]=0;
				res=min(res,f(w,h,m2,cx-dx[i],cy-dy[i],t+1));
				break;
			}
			d++;
		}
	}
	return res;
}

int main(){
	while(cin>>w>>h&&w){
		rep(i,h)rep(j,w){
			cin>>m[i][j];
			if(m[i][j]==2){sx=j,sy=i;}
			if(m[i][j]==3){gx=j,gy=i;}
		}
		int ans=f(w,h,m,sx,sy,0);
		cout<<(ans<99?ans:-1)<<endl;
	}
}

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

using i64 = int64_t;

const i64 MOD = 1e9+7;

const i64 INF = 1e18+7;

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

template<typename T>
struct Segtree{
	const int n;
	const T op;
	vector<T> elm;
	function<T(T, T)> f;

	Segtree(int n, T init, function<T(T, T)> f, T op = T()) :
			n(n),
			op(op),
			f(f),
			elm(2 * n, init)
	{
	}

	void set(int x, T val){
		x += n;
		elm[x] = val;
		while(x >>= 1)
			elm[x] = f(elm[2 * x], elm[2 * x + 1]);
	}

	void update(int x, T val){
		x += n;
		elm[x] = f(val, elm[x]);
		while(x >>= 1)
			elm[x] = f(elm[2 * x], elm[2 * x + 1]);
	}

	T get(int x, int y) const{
		T val = op;
		for(x += n, y += n - 1; x <= y; x >>= 1, y >>= 1){
			if(x & 1)
				val = f(val, elm[x++]);
			if(!(y & 1))
				val = f(val, elm[y--]);
		}
		return val;
	}
};

signed main(){

	int n, d;
	cin >> n >> d;
	vector<int> v(n);
	for(auto& x : v)
		cin >> x;

	auto f = [](pair<i64,int> x, pair<i64,int> y){
		return min(x, y);
	};

	Segtree<pair<i64,int>> seg_a(n, pair<i64,int>(), f, pair<i64,int>(INF, 0));
	Segtree<pair<i64,int>> seg_b(n, pair<i64,int>(), f, pair<i64,int>(INF, 0));

	auto s = [&](int i){
		seg_a.set(i, make_pair(v[i] + i64(1) * d * i, i));
		seg_b.set(i, make_pair(v[i] + i64(1) * d * (n - i - 1), i));
	};

	auto t = [&](int i){
		seg_a.set(i, make_pair(INF, i));
		seg_b.set(i, make_pair(INF, i));
	};

	for(int i = 0; i < n; ++i)
		s(i);

	UnionFind u(n);

	i64 ans = 0;

	while(u.par[u.Find(0)] != -n){
		// 子ノードの一覧
		vector<vector<int>> ch(n);
		// 接合するノード
		vector<pair<i64, int>> nex(n, make_pair(INF, 0));

		for(int i = 0; i < n; ++i)
			ch[u.Find(i)].push_back(i);

		for(int i = 0; i < n; ++i){
			if(ch[i].empty())
				continue;
			for(auto& x : ch[i])
				t(x);

			pair<i64,int> val(INF, 0);

			for(auto& x : ch[i]){
				auto p_a = seg_a.get(x + 1, n);
				p_a.first -= i64(1) * d * x;
				p_a.first += v[x];
				val = f(val, p_a);

				auto p_b = seg_b.get(0, x);
				p_b.first -= i64(1) * d * (n - 1 - x);
				p_b.first += v[x];
				val = f(val, p_b);
			}

			for(auto& x : ch[i])
				s(x);

			nex[i] = val;
		}

		for(int i = 0; i < n; ++i){
			if(nex[i].first != INF && u.Unite(i, nex[i].second))
				ans += nex[i].first;
		}
	}
	cout << ans << endl;
}


#include <iostream>
#include <vector>
#include <math.h>

using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;

void printDP(vvi &dp){
    for(auto v: dp){
        for(auto x: v){
            cout << x << " ";
        }
        cout << endl;
    }
    cout << endl;
}

void inc(bitset<15> &a){
    a = a.to_ullong() +1;
}

int main() {
    int n;
    cin >> n;
    vector<vector<pair<int,int>>> testimonies(n);
    vvi dp(n,vi(n,-1));
    for(int i = 0 ; i < n ; i ++ ){
        int q;
        cin >> q;
        vector<pair<int,int>> xy(q);
        for(int j = 0 ; j < q;j++){
            int f,s;
            cin >> f>> s;
            f--;

            xy[j] = {f,s};
            dp[f][i] = s;
        }
        testimonies[i]  = xy;
    }

//    printDP(dp);
    bitset<15> bp;
    int maxScore = 0;
    while (bp.count() < n){
        inc(bp);
        bool isValid = true;
        for(int i = 0 ; i <n;i++ ){
            isValid = true;
            if(bp[i]){
                for(int j = 0 ; j < n ; j++){
                    if(dp[i][j] == 0 && bp[j]){
                        isValid = false;
                        break;
                    }
                }
            }else{
                for(int j = 0 ; j < n ; j++){
                    if(dp[i][j] == 1 && bp[j]){
                        isValid = false;
                        break;
                    }
                }
            }
            if(!isValid){
                break;
            }
        }
        if(isValid){
            maxScore = max(maxScore,(int)bp.count());
        }
    }
    cout << maxScore;
    return 0;
}

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

int counter(int bits) {
    if (bits == 0) return 0;
    return counter(bits >> 1) + (bits & 1);
}

int main(int argc, char const *argv[]) {
    int n;
    cin >> n;
    vector<int> a(n+1);
    vector<vector<int> > x(n+1, vector<int>(n+1));
    vector<vector<int> > y(n+1, vector<int>(n+1));
    for (size_t i = 1; i <= n; i++) {
        cin >> a[i];
        for (size_t j = 1; j <= a[i]; j++) {
            cin >> x[i][j] >> y[i][j];
        }
    }

    int ans = 0;
    for (int bits = 1; bits < (1<<n); bits++) {
        bool can = true;
        for (int i = 1; i <= n; i++) {
            if (bits & 1<<(i-1)) {
                for (size_t j = 1; j <= a[i]; j++) {
                    if (((bits >> (x[i][j]-1)) & 1) ^ y[i][j]) can = false;
                }
            }
        }
        if (can) ans = max(ans, counter(bits));
    }

    cout << ans << endl;
    return 0;
}


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

const int N_MAX=31;
int dp[N_MAX];

int rec(int n){
    int res;
    if(n<0)return 0;
    if(n==0)return 1;
    if(dp[n]>=0){
        return dp[n];
    }else{
        res=rec(n-1)+rec(n-2)+rec(n-3);
    }
    return dp[n]=res;
}

int main(){
    int n;
    while(cin>>n){
        memset(dp,-1,sizeof(dp));
        if(n==0)break;
        cout<<ceil(rec(n)/3650.0)<<endl;
    }
}


#include<bits/stdc++.h>
#define mo 998244353
#define pi 3.1415926535898
#define eps 1e-9
using namespace std;
long long read(){
    long long xx=0,flagg=1;
    char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-')
        ch=getchar();
    if(ch=='-'){
        flagg=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        xx=xx*10+ch-'0';
        ch=getchar();
    }
    return xx*flagg;
}
void pus(long long xx,long long flagg){
    if(xx<0){
        putchar('-');
        xx=-xx;
    }
    if(xx>=10)
        pus(xx/10,0);
    putchar(xx%10+'0');
    if(flagg==1)
        putchar(' ');
    if(flagg==2)
        putchar('\n');
    return;
}
long long n,i,j,r[1000005],top,x[1000005],y[1000005];
long long head,tail,sum,max1,zhan[1000005];
char s[1000005],t[1000005];
int main(){
	n=read();
	scanf("%s%s",s+1,t+1);
	for(i=1;i<=n;i++)
		if(s[i]!=t[i])
			break;
	if(i==n+1){
		pus(0,2);
		return 0;
	}
	j=n;
	for(i=n;i>=1;i--){
		if(i<j)
			j=i;
		while(j!=0&&t[i]!=s[j])
			j--;
		if(j==0){
			pus(-1,2);
			return 0;
		}
		if(r[j]==0)
			r[j]=i;
	}
	for(i=1;i<=n;i++)
		if(r[i]!=0){
			top++;
			x[top]=i;
			y[top]=r[i];
		}
	head=1;
	for(i=top;i>=1;i--){
		while(head<=tail&&y[i]<zhan[head]-sum)
			head++;
		tail++;
		sum++;
		zhan[tail]=x[i]+sum;
		max1=max(max1,tail-head+1);
	}
	pus(max1,2);
	return 0;
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll,ll>;

#define INF (1LL << 60)
#define MOD 998244353
#define PI 3.14159265358979323846

#define REP(i,m,n) for(ll (i)=(m),(i_len)=(n);(i)<(i_len);++(i))
#define FORR(i,v) for(auto (i):v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x) ASC((x)); REV((x))
#define pb push_back
#define eb emplace_back

ll modpow(ll a, ll n, ll m)
{
    if(n == 0) return 1;
    ll t = modpow(a, n >> 1, m);
    t = t * t % m;
    if(n & 1) t = t * a % m;
    return t;
}

int main()
{
    ll N;
    cin >> N;
    vec D(N);
    REP(i,0,N) cin >> D[i];

    vec A(N, 0);
    if(D[0] != 0) PR(0);
    else {
        ll m = 0;
        REP(i,0,N) {
            ++A[D[i]];
            m = MAX(m,D[i]);
        }
        if(A[0] > 1) PR(0);
        else {
            ll cnt = 1;
            REP(i,1,m+1) {
                cnt = (cnt * modpow(A[i-1], A[i], MOD)) % MOD;
            }
            PR(cnt);
        }
    }

    return 0;
}

/*

*/


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll) 1e15;

ll calc(ll &m, ll p) {
    ll ret = 1;
    m += p;
    if (m >= 10) {
        ret++;
        m = (m / 10) + (m % 10);
    }
    return ret;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll M;
    cin >> M;
    ll ans = 0;
    vector<ll> v(M);
    REP(i, M) {
        ll d, c;
        cin >> d >> c;

        vector<ll> u;
        while (c > 1) {
            if (c % 2 == 1)
                u.push_back(d);
            c /= 2;
            ans += c;
            d = d + d;
            if (d >= 10) {
                d = (d / 10) + (d % 10);
                ans += c;
            }
        }
        u.push_back(d);
        ll n = u[0];
        REP(j, u.size() - 1) {
            ans += calc(n, u[j + 1]);
        }
        v[i] = n;
    }

    ll m = v[0];
    REP(i, M - 1) {
        ans += calc(m, v[i + 1]);
    }
    cout << ans << endl;
    return 0;
}

#include <iostream>
using namespace std;
using ll = long long;

int main() {
    int n;
    cin >> n;
    ll s=0, t=0;
    for(int i=0; i<n; i++) {
        ll x,y;
        cin >> x >> y;
        s += x*y;
        t += y;
    }
    ll k=s%9;
    if(k==0) k=9;
    cout << t-1+(s-k)/9 << endl;
}


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

int n;
map<char, int> mp;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

int main(){
//	cin.tie(0);
//	ios::sync_with_stdio(false);
	mp['N'] = 0;
	mp['S'] = 1;
	mp['E'] = 2;
	mp['W'] = 3;
	while(1){
		cin >> n;
		if(n == 0) return 0;
		int a[25][25] = {{}};
		for(int i=0; i<n; ++i){
			int x, y;
			cin >> x >> y;
			a[x][y] = 1;
		}
		int m;
		cin >> m;
		int p = 10, q = 10;
		int tmp = 0;
		if(a[p][q] == 1) ++tmp;
		for(int i=0; i<m; ++i){
			char c;
			int d;
			cin >> c >> d;
			for(int j=0; j<d; ++j){
				p += dx[mp.at(c)];
				q += dy[mp.at(c)];
				if(a[p][q] == 1){
					++tmp;
					a[p][q] = 0;
				}
			}
		}
		cout << (tmp == n ? "Yes\n" : "No\n");
	}
}

//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }

//math
//-------------------------------------------
template<class T> inline T sqr(T x) { return x*x; }

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
int mapgem[21][21];
VI wque;
vector<char> que;
VI gemx;
VI gemy;
string search(){
	//N y+ S y-
	//E x+ W x-
	int rx = 10;
	int ry = 10;
	int dx = 0;
	int dy = 0;
	int qsize = wque.size();
	mapgem[rx][ry]=0;
	REP(i,qsize){
		dx = 0;
		dy = 0;
		if(que[i] == 'N'){dy++;}
		else if(que[i] == 'S'){dy--;}
		else if(que[i] == 'E'){dx++;}
		else if(que[i] == 'W'){dx--;}
		REP(j,wque[i]){
			rx += dx;
			ry += dy;
			mapgem[rx][ry] = 0;
		}
	}
	REP(i,gemx.size()){
		if(mapgem[gemx[i]][gemy[i]]==1){return "No";}
	}
	return "Yes";
}
int main(){
	int endlchk =8101919;
	int	N;
	int M;
	char d;
	int l;
	int mx,my;
	while(true){
		if(endlchk != 8101919){cout << endl;}
		endlchk = 0;
		cin >> N;
		if(N==0){break;}
		REP(i,20){
			REP(j,20){
				mapgem[i][j]=0;
			}
		}
		REP(i,N){
			cin >> mx >> my;
			mapgem[mx][my] = 1;
			gemx.push_back(mx);
			gemy.push_back(my);
		}
		cin >> M;
		REP(i,M){
			cin >> d >> l;
			que.push_back(d);
			wque.push_back(l);
		}

		cout << search();
		que.clear();
		wque.clear();
		gemx.clear();
		gemy.clear();
	}
	return 0;
}


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

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

    if(n % 111 == 0) {
        cout << n << endl;
        return 0;
    }
    else {
        cout << 111 * ((n / 111)+1) << endl;
    }
}

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i,n) for(int i=0, i##_len=(int)(n); i<i##_len; i++)
#define reps(i,n) for(int i=1 , i##_len=(int)(n);i<=i##_len;i++)
#define rrep(i,n) for(int i=((int)(n)-1);i>=0;i--)
#define rreps(i,n) for(int i=((int)(n));i>0;i--)
#define repi(i,x) for(auto i=(x).begin(),i##_fin=(x).end();i!=i##_fin;i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define solve(a) ((a)?"Yes":"No")
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int , int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long , long long> P;
typedef vector<P> VP;
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;}
const long long INFLL = 1LL<<60;
const int INF = 1<<30;
const double PI=acos(-1);
int main(){
	string s;
	string ans="";
	cin >>s;
	for(int i=0;i<s.size()-8;i++){
		ans+=s[i];
	}
	cout<<ans<<endl;
}


#pragma region template
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vvvld = vector<vvld>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
template <typename T>
using pqrev = priority_queue<T, vector<T>, greater<T>>;
#define rep(i, n) for (ll i = 0, i##_end = (n); i < i##_end; i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repr(i, a, b) for (ll i = (a), i##_end = (b); i < i##_end; i++)
#define reprb(i, a, b) for (ll i = (b)-1, i##_end = (a); i >= i##_end; i--)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
//*
constexpr ll MOD = 1e9 + 7;
/*/
constexpr ll MOD = 998244353;
//*/
constexpr ll INF = 1e+18;
constexpr ld EPS = 1e-12L;
constexpr ld PI = 3.14159265358979323846L;
constexpr ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
constexpr ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
template <typename S, typename T>
constexpr bool chmax(S &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename S, typename T>
constexpr bool chmin(S &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif
template <typename T>
bool print_(const T &a) {
  cout << a;
  return true;
}
template <typename T>
bool print_(const vector<T> &vec) {
  for (auto &a : vec) {
    cout << a;
    if (&a != &vec.back()) {
      cout << " ";
    }
  }
  return false;
}
template <typename T>
bool print_(const vector<vector<T>> &vv) {
  for (auto &v : vv) {
    for (auto &a : v) {
      cout << a;
      if (&a != &v.back()) {
        cout << " ";
      }
    }
    if (&v != &vv.back()) {
      cout << "\n";
    }
  }
  return false;
}
void print() { cout << "\n"; }
template <typename Head, typename... Tail>
void print(Head &&head, Tail &&... tail) {
  bool f = print_(head);
  if (sizeof...(tail) != 0) {
    cout << (f ? " " : "\n");
  }
  print(forward<Tail>(tail)...);
}
#pragma endregion

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int n, m, q;
  cin >> n >> m >> q;
  vs s(n);
  rep(i, n){
    cin >> s[i];
  }
  vvi v(n+1, vi(m+1, 0));
  vvi ev(n, vi(m+1, 0));
  vvi eh(n+1, vi(m, 0));
  rep(i, n)rep(j, m){
    v[i+1][j+1] = s[i][j]-'0';
  }
  rep(i, n+1)rep(j, m){
    v[i][j+1] += v[i][j];
  }
  rep(i, n)rep(j, m+1){
    v[i+1][j] += v[i][j];
  }
  rep(i, n-1)rep(j, m){
    ev[i+1][j+1] = (s[i][j] == '1' && s[i+1][j] == '1');
  }
  rep(i, n)rep(j, m){
    ev[i][j+1] += ev[i][j];
  }
  rep(i, n-1)rep(j, m+1){
    ev[i+1][j] += ev[i][j];
  }
  rep(i, n)rep(j, m-1){
    eh[i+1][j+1] = (s[i][j] == '1' && s[i][j+1] == '1');
  }
  rep(i, n+1)rep(j, m-1){
    eh[i][j+1] += eh[i][j];
  }
  rep(i, n)rep(j, m){
    eh[i+1][j] += eh[i][j];
  }
  while(q--){
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int ans = v[c][d]-v[a-1][d]-v[c][b-1]+v[a-1][b-1];
    ans -= ev[c-1][d]-ev[a-1][d]-ev[c-1][b-1]+ev[a-1][b-1];
    ans -= eh[c][d-1]-eh[a-1][d-1]-eh[c][b-1]+eh[a-1][b-1];
    print(ans);
  }
}

#include<bits/stdc++.h>
using namespace std;
bool comp(pair<long long int,long long int>a,pair<long long int,long long int>b)
{
    if(a.first==b.first)
    {
        if(a.second<=b.second)
        return true;
        else
        return false;
    }
    if(a.first>b.first)
    return true;
    else return false;
}
long long int gcd(long long int bdda,long long int chota)
{
    if(chota==0)
        return bdda;
    return gcd(chota,bdda%chota);
}
void solve()
{
    int A, B, M;
    cin >> A >> B >> M;
    vector<int> a(A);
    vector<int> b(B);
    for (int i = 0; i < A; i++)cin >> a[i];
    for (int i = 0; i < B; i++)cin >> b[i];
    int minA = *min_element(a.begin(), a.end());
    int minB = *min_element(b.begin(), b.end());
    int ans = minA+minB;
    for (int i = 0; i < M; i++) {
    int x, y, c;
    cin >> x >> y >> c; x--; y--;
    ans = min(ans, a[x] + b[y] - c);
    }
    cout << ans << endl;
    return;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    t=1;

    //cin>>t;
    while(t--)
    {
        solve();
    }
}


#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
typedef long long ll;
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REP(i,num,n) for(int i=num, i##_len=(n); i<i##_len; ++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; }
#define print(x) (cout << (x) << endl)
#define pb push_back
#define mp make_pair
#define sz(x) int(x.size())
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class itr> void cins(itr first, itr last){ for(auto i = first; i != last; i++){ cin >> (*i); } }
template<class itr> void array_output(itr start, itr goal){ string ans; for(auto i = start; i != goal; i++) ans += to_string(*i) + " "; if(!ans.empty()) ans.pop_back(); cout << ans << endl; }
#define fs first
#define sc second

const int INF = 1e9;
const ll LLINF = 1e16;
const ll MOD = 1e9+7;
template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

// 指定した文字cが文字列に何文字入ってるか
ll stringcount(string s, char c) {
    return count(s.cbegin(), s.cend(), c);
}

// 割り算の切り上げ処理
template<typename T>
T rp(T a, T b) {
  return (a + b - 1) / b;
}

// 桁和
template<typename T>
T digsum(T n) {
    T res = 0;
    while(n > 0) {
        res += n%10;
        n /= 10;
    }
    return res;
}

// 回文判定
bool kai (string s) {
    string t{s};
    reverse(begin(t), end(t));
    return s == t;
}

using pint = pair<int, int>;
using pll = pair<ll, ll>;

int main(void)
{
    cout.tie(0);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    int n;
    cin >> n;
    print(180 * (n - 2));

}

#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<n; i++)
#define rep1(i, n) for(int i=1; i<=n; i++)
#define repr(i, n) for(int i=n-1; i>=0; i--)
#define repr1(i, n) for(int i=n; i>=1; i--)
#define all(v) v.begin(),v.end()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = 1e9+7;
const double EPS = 1e-10;
const double PI = acos(-1);
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

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

    map<ll, ll> m;
    rep(i, n) m[a[i]]++;

    ll ans = 0;
    rep(i, n) {
        if (!m[a[i]]) continue;
        m[a[i]]--;
        ll p = 1;
        while (p <= a[i]) p *= 2;
        if (m[p - a[i]]) {
            m[p - a[i]]--;
            ans++;
        }
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <class T>inline bool chmax(T &a, T b){if (a < b){a = b;return true;}return false;}
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Pl = pair<long long, long long>;
using veci = vector<int>;
using vecl = vector<long long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;

int main() {
    ll x; cin >> x;
    ll ans = x/11*2;
    ll rest = x%11;
    if(rest >= 7) ans += 2;
    else if(rest > 0) ans++;
    cout << ans << endl;
}

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

int main() {
  int a, x, y;
  cin >> a;
  x = a;

  // ここにプログラムを追記
  while (x > 0) {
    y += x % 10;
    x /= 10;
  }
  if ((a % y) == 0) {
    cout << "Yes" << endl;
  }
  else {
    cout << "No" << endl;
  }
}


#include <iostream>

using namespace std;

int order[4000];

bool search(int);

class Member {
public:
	Member() {sum = 0;}

	void add(long price, long num) {
		sum += (long long int)price*num;
	}

	bool milion()
	{
		if (sum >= 1000000) return true;
		else return false;
	}

private:
	long long int sum;
	int id;
};

bool search(int num)
{
	for (int i=0; i<4000; i++)
		if (order[i] == num ) return true;

	return false;

}

int main()
{
	int n, idx, memNum;
	long price, num;
	bool nothing;

	ios::sync_with_stdio();

	while (1) {
		Member *member = new Member[4000];
		memNum=0;
		nothing =true;

		for (int i=0; i<4000; i++) order[i] = -1;

		if (cin >> n, !n) break;
		for (int i=0; i<n; i++) {
			scanf("%d %ld %ld", &idx, &price, &num);
			member[idx].add(price, num);
			if (!search(idx)) order[memNum++] = idx;
		}

		for (int i=0; i<4000 && order[i] > 0; i++)
			if (member[order[i]].milion()) { cout << order[i] << endl; nothing = false; }
		if (nothing) cout << "NA" << endl;
	}
}

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

using namespace std;

int findNum(vector<pair<int, long long> > comm, int num) {
    for(int i=0; i<comm.size(); i++)  {
      if(comm[i].first == num) {
	return i;
      }
    }
    return comm.size();
  }

int main() {
  int n;
  cin>>n;
  while(n != 0) {
    int m = n;
    vector<pair<int, long long> > comm;
    while(m--) {
      int num;
      long long price, unit;
      cin>>num>>price>>unit;
      int dataNum = findNum(comm, num);
      if(dataNum == comm.size()) {
        comm.push_back(make_pair(num, price*unit));
      }
      else {
        comm[dataNum].second += price*unit;
      }
    }

    bool flag = true;
    for(int i=0; i<comm.size(); i++) {
      if(comm[i].second >= 1000000) {
        cout<<comm[i].first<<endl;
        flag = false;
      }
    }
    if(flag == true) {
      cout<<"NA"<<endl;
    }
    cin>>n;
  }
}


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

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 (a>b) { a=b; return 1; } return 0; }
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()

#define INF 1000000009
#define MAX_V 500000

struct edge {
    int to;
    int cost;
    edge(int to,int cost):to(to),cost(cost){}
};

int V;
vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s) {
    priority_queue<P, vector<P>, greater<P> > que;
    fill(d, d+V, INF);
    d[s] = 0;
    que.push(P(0, s));

    while (!que.empty()) {
        P p = que.top();
        que.pop();
        int v = p.second;
        if (d[v] < p.first) continue;

        for (int i=0; i<G[v].size(); ++i) {
            edge e = G[v][i];
            if (d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                que.push(P(d[e.to], e.to));
            }
        }
    }
}

map<P,int> zip(vector<P> v){
  sort(all(v));
  map<P,int> res;
  int vsz=v.size();
  rep(i,vsz){
    res[v[i]]=i;
  }
  return res;
}

int main(){
  int n,m;cin>>n>>m;
  V=500000;
  int p[m],q[m],c[m];
  rep(i,m){
    cin>>p[i]>>q[i]>>c[i];
    --p[i];--q[i];--c[i];
  }
  vector<P> tozip;
  rep(i,m){
    tozip.push_back(P(p[i],c[i]));
    tozip.push_back(P(q[i],c[i]));
  }
  rep(i,n){
    tozip.push_back(P(i,-1));
  }
  auto index=zip(tozip);
  rep(i,m){
    G[index[P(p[i],c[i])]].push_back(edge(index[P(q[i],c[i])],0));
    G[index[P(q[i],c[i])]].push_back(edge(index[P(p[i],c[i])],0));
    G[index[P(p[i],c[i])]].push_back(edge(index[P(p[i],-1)],1));
    G[index[P(q[i],c[i])]].push_back(edge(index[P(q[i],-1)],1));
    G[index[P(p[i],-1)]].push_back(edge(index[P(p[i],c[i])],1));
    G[index[P(q[i],-1)]].push_back(edge(index[P(q[i],c[i])],1));
  }
  dijkstra(0);
  cout<<(d[index[P(n-1,-1)]]==INF?-1:d[index[P(n-1,-1)]]/2)<<endl;
}

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

typedef long long ll;

const int MAXV = 100;
const ll INF = (1LL<<60);

int V, E;
ll adjMat[MAXV][MAXV];

int main(){

    scanf("%d%d",&V,&E);
    for (int i = 0; i < V; i++)
        for(int j = 0; j < V; j++)
            adjMat[i][j] = INF;

    for (int i = 0, s, t, d; i < E; i++) {
        scanf("%d%d%d",&s,&t,&d);

        adjMat[s][t] = d;
    }

    for (int k = 0; k < V; k++)
        for (int i = 0; i < V; i++)
            for (int j = 0;j < V; j++)
                if (adjMat[i][k] != INF && adjMat[k][j] != INF)
                    adjMat[i][j] = min(adjMat[i][j],adjMat[i][k]+adjMat[k][j]);

    for (int i = 0; i < V; i++) {
        if (adjMat[i][i] < 0) {
            printf("NEGATIVE CYCLE\n");

            return 0;
        }
    }

    for (int i = 0; i < V; i++){
        for (int j = 0; j < V; j++){
            if (j)
                printf(" ");
            if (i == j)
                printf("0");
            else if (adjMat[i][j] >= INF)
                printf("INF");
            else
                printf("%lld",adjMat[i][j]);
        }
        printf("\n");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int gcd(int x,int y){
  if(x%y==0) return y;
  else return gcd(y,x%y);
}
int main(){
  int n,i,k,a,G,MAX;scanf("%d%d",&n,&k);
  scanf("%d",&G);MAX = G;
  if(n == 1){
    printf((k==G) ? "POSSIBLE\n":"IMPOSSIBLE\n");
    return 0;
  }
  for(i=1;i<n;i++){
    scanf("%d",&a);
    G = gcd(G,a);
    MAX = max(MAX,a);
  }
  printf((k<=MAX && k%G==0) ? "POSSIBLE\n":"IMPOSSIBLE\n");
}

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

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

#define MOD 1000000007

#define rep(i, n) for(ll (i) = 0LL;(i) < (ll)(n);(i)++)
#define rep2(i, s, e) for(ll (i) = (ll)(s);(i) < (ll)(e);(i)++)
#define repi(i, n) for(ll (i) = 0LL;(i) <= (ll)(n);(i)++)
#define repi2(i, s, e) for(ll (i) = (ll)(s);(i) <= (ll)(e);(i)++)
#define per(i, n) for(ll (i) = (ll)(n) - 1LL;(i) >= 0LL;(i)--)
#define per2(i, s, e) for(ll (i) = (ll)(s) - 1LL;(i) >= (ll)(e);(i)--)
#define peri(i, n) for(ll (i) = (ll)(n);(i) >= 0LL;(i)--)
#define peri2(i, s, e) for(ll (i) = (ll)(s);(i) >= (ll)(e);(i)--)
#define iter(i, it) for(auto &(i): (it))

template<typename T, typename U> ostream& operator<<(ostream &s, const pair<T, U> m) {
    cout << "(" << m.first << ", " << m.second << ")";
    return s;
}
template<typename T, typename U> ostream& operator<<(ostream &s, const map<T, U> m) {
    ll c = 0;
    cout << "{ ";
    iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
    cout << "{ ";
    rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const list<T> &v) {
    ll c = 0;
    cout << "{ ";
    iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}

int main(void) {
    ll N, K;
    vector<ll> A;
    cin >> N >> K;
    A.resize(N);
    rep(i, N) cin >> A[i];
    if(N == 1) {
        if(A[0] == K) cout << "POSSIBLE" << endl;
        else cout << "IMPOSSIBLE" << endl;
    } else {
        ll mini;
        ll prev;
        sort(A.begin(), A.end());
        mini = A[1] - A.front();
        prev = A.front();
        while(mini != prev) {
            ll temp;
            temp = mini;
            mini = abs(prev - mini);
            prev = temp;
            if(mini == 0) {
                mini = prev;
                break;
            }
        }
        iter(a, A) {
            if(a >= K && (a - K) % mini == 0) {
                cout << "POSSIBLE" << endl;
                return 0;
            }
        }
        cout << "IMPOSSIBLE" << endl;
    }
    return 0;
}


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

using namespace std;

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

	if (R < 1200) {
		cout << "ABC" << endl;
	}
	else if (R < 2800) {
		cout << "ARC" << endl;
	}
	else {
		cout << "AGC" << endl;
	}
}

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <math.h>
#include <complex>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <bitset>
#include <functional>
#include <assert.h>
#define int long
#define ll long long
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define eb emplace_back
#define all(x) x.begin(),x.end()
#define SZ(x) x.size()
#define fi first
#define se second
const int INF = 1e9;
using namespace std;

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

using pi=pair<int,int>;
using vi=vc<int>;

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

    int N; cin >> N;
    cout << (N-2)*180 << endl;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <assert.h>
#include <memory.h>
#include <queue>
using namespace std;

#define N 303

int dp[N][N][N];

char s[N];
void solve() {
  scanf(" %s",s+1);
  int n = strlen(s+1);
  int k;scanf("%d",&k);
  for(int cc = k;cc>=0;--cc) {
    for(int i=1;i<=n;++i) {
      for(int j=n;j>=i+1;--j) {
        dp[cc][i+1][j] = max(dp[cc][i+1][j], dp[cc][i][j]);
        dp[cc][i][j-1] = max(dp[cc][i][j-1],dp[cc][i][j]);
        if(s[i]==s[j]) {
          dp[cc][i+1][j-1] = max(dp[cc][i+1][j-1], dp[cc][i][j]+1);
        } else if(cc>0){
          dp[cc-1][i+1][j-1] = max(dp[cc-1][i+1][j-1], dp[cc][i][j]+1);
        }
      }
    }
  }
  int ret = 0;
  for(int i=1;i<=n;++i) {
    for(int cc=0;cc<=k;++cc) {
      ret = max(ret, dp[cc][i][i-1]*2);
      ret = max(ret, dp[cc][i+1][i]*2);
      ret = max(ret,1 + dp[cc][i][i]*2);
    }
  }
  printf("%d\n", ret);
}

int main() {
  //freopen("input.txt","r",stdin);
  solve();
  return 0;
}

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

signed main(){
    FASTER;
    int n;cin>>n;
    cout<<180*(n-2);
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll GCD(ll x,ll y){
    if(y == 0) return x;
    else return GCD(y,x%y);
}

int main() {
    int n;
    cin >> n;
    cout << n / 3 << endl;
}

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

using namespace std;

int main()
{
    int a;

    cin>>a;
    a += (a*a) + (a*a*a);
    cout<<a;
    cout<<endl;
    return 0;
}


#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
  int N;
  cin>>N;
  int A[200000],B[200000];
  for(int i=0;i<N;i++) cin>>A[i];
  for(int i=0;i<N;i++) cin>>B[i];
  sort(B,B+N,greater<int>());
  vector<int> same;
  for(int i=0;i<N;i++){
    if(A[i]==B[i]) same.push_back(i);
  }
  int j=0;
  int judge=0;
  for(int i=0;i<same.size();i++){
    while(A[same[i]]==B[same[i]]){
      if(j==N){
        judge++; break;
      }
      if(A[j]!=B[same[i]]&&B[j]!=B[same[i]]) swap(B[same[i]],B[j]);
      j++;
    }
    if(judge==1) break;
  }
  if(judge==0){
    cout<<"Yes"<<endl;
    for(int i=0;i<N-1;i++) cout<<B[i]<<' ';
    cout<<B[N-1]<<endl;
  }
  else cout<<"No"<<endl;

  return 0;
}

#include <iostream>
#include <string>
#include <queue>

using namespace std;

class Player{
private:
  int  base;

public:
  Player(int i){
    base=1;
  }

  void SetBase(int a){
    this->base =a;
  }

  int GetBase(){
    return this->base;
  }
};

class Baseball{
private:
  int score;
  int out;
  queue<Player> que;

public:
  Baseball(){
      score = out=0;
  }

  void add_hit(int num){
    Player one(1);
    this->que.push(one);
    int length=this->que.size();

    for(int i= 0;i<length;i++){
      Player tmp = this->que.front();
      this->que.pop();
      int tmp_base=tmp.GetBase();
      if((tmp_base+num)>4){
        score++;
      }else{
        tmp.SetBase(tmp_base+num);
        this->que.push(tmp);
      }
    }
  }

  void add_out(){
    this->out++;
  }

  void add_score(){
    this->score++;
  }

  int GetOut(){
    return this->out;
  }

  int GetScore(){
    return this->score;
  }

  void show_score(){
    cout<<this->score<<endl;
  }

};

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

  for(int i=0;i<time;i++){
    Baseball baseball;
    while(baseball.GetOut()!=3){
      string judge;
      cin>>judge;
      if(judge == "HIT"){
        baseball.add_hit(1);
      }else if(judge=="OUT"){
        baseball.add_out();
      }else if(judge=="HOMERUN"){
        baseball.add_hit(4);
      }

    }

    cout<<baseball.GetScore()<<endl;
  }

  return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define rep(i,n) for(int i=0;i<(lint)(n);i++)
#define REP(i,n) for(int i=1;i<=(lint)(n);i++)
#define all(V) V.begin(),V.end()
#define stackReplaceBegin(size)\
	void* p=malloc(size);\
	esp_new=(lint)p+size-1;\
	void * stack_extend_memory_ = malloc(size);\
	void * stack_extend_origin_memory_;\
	char * stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16);\
	*stack_extend_dummy_memory_ = 0;\
	asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024));
typedef long long lint;
typedef unsigned long long ulint;
typedef std::pair<int, int> P;
constexpr int INF = INT_MAX/2;
constexpr lint LINF = LLONG_MAX/2;
constexpr double eps = DBL_EPSILON;
constexpr double PI = 3.141592653589793238462643383279;
lint esp_new;
template<class T>
class prique :public std::priority_queue<T, std::vector<T>, std::greater<T>> {};
template <class T, class U>
inline bool chmax(T& lhs, const U& rhs) {
	if (lhs < rhs) {
		lhs = rhs;
		return 1;
	}
	return 0;
}
template <class T, class U>
inline bool chmin(T& lhs, const U& rhs) {
	if (lhs > rhs) {
		lhs = rhs;
		return 1;
	}
	return 0;
}
inline lint gcd(lint a, lint b) {
	while (b) {
		lint c = a;
		a = b; b = c % b;
	}
	return a;
}
inline lint lcm(lint a, lint b) {
	return a / gcd(a, b) * b;
}
bool isprime(lint n) {
	if (n == 1)return false;
	for (int i = 2; i * i <= n; i++) {
		if (n % i == 0)return false;
	}
	return true;
}
template<typename T>
T mypow(T a, unsigned int b) {
	T res(1),now(a);
	while(b){
		if(b&1)res*=now;
		b>>=1;
		now*=now;
	}
	return res;
}
template<typename T>
void printArray(std::vector<T>& vec) {
	rep(i, (int)vec.size() - 1)std::cout << vec[i] << " ";
	if(!vec.empty())std::cout << vec.back() << std::endl;
}
template<typename T>
void printArray(T l, T r) {
	T rprev = r;
	rprev--;
	for (T i = l; i != rprev; i++) {
		std::cout << *i << " ";
	}
	std::cout << *rprev << std::endl;
}
int m;
int main(){
	lint sum=0,digits=0;
	std::cin>>m;
	rep(i,m){
		lint d,c;
		std::cin>>d>>c;
		sum+=d*c;
		digits+=c;
	}
	std::cout<<digits-1+(sum-1)/9<<std::endl;
	return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i{ 0 }; i < n; ++i)

using namespace std;

int main() {
    int d, g;
    cin >> d >> g;
    vector<int> ps(d);
    vector<int> cs(d);

    rep(i, d) cin >> ps[i] >> cs[i];
    // for(auto p :cs) cout <<p;

    int min_cnt{ INT_MAX };
    rep(i, 1 << d) {
        int cnt{ 0 };
        int score{ 0 };
        rep(j, d) if (i >> j & 1) {
            score += ps.at(j) * 100 * (j + 1) + cs.at(j);
            cnt += ps.at(j);
        }
        // cout << "i: " << i << "cnt: " << cnt << "score: " << score << endl;

        for (int j = d - 1; j >= 0; j--) {
            if (score >= g) continue;
            if (i >> j & 1) continue;
            int q = min((int)((g - score) / ((j + 1) * 100)), ps.at(j));
            score += q * 100 * (j + 1);
            cnt += q;
            // cout << "q: " << q << "cnt: " << cnt << "score: " << score << endl;
        }
        // cout << "i: " << i << "cnt: " << cnt << "score: " << score << endl;

        if (score >= g) min_cnt = min(min_cnt, cnt);
    }

    cout << min_cnt << endl;
}


#include <bits/stdc++.h>
#define fastIO (cin.tie(0), cout.tie(0), ios::sync_with_stdio(false))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define precise(i) fixed << setprecision(i)
using lint = long;
using llint = long long;
using namespace std;

int main() {
    fastIO;
    string s;
    cin >> s;
    for (size_t i = 0; i < s.size() - 1; i++) {
        if (s.at(i) == 'A' && s.at(i + 1) == 'C') {
            cout << "Yes\n";
            return 0;
        }
    }
    cout << "No\n";
}

#include <iostream>
#include <numeric>
#include <math.h>
#include <algorithm>
#include <float.h>
#include <limits>
#include <vector>
#include <string.h>
#include <iomanip>

#define rep(i,a,n) for(ll int (i) = (a);(i) < (n);(i)++)
#define urep(i,a,n) for(ll int (i) = (a);(i) > (n);(i)--)
#define MOD 1000000007
#define INF 2147483647
#define ll long long
#define asort(a) sort(a.begin(),a.end());//昇順ソート
#define usort(a) sort(a.rbegin(),a.rend());//降順ソート

using namespace::std;

int jo(ll int a, ll int b){//累乗
    ll int num=1;
    rep(i,0,b) num=num*a;
    return num;
}
char tobig(char s){//char大文字化
  return (s-0x20);
}
char tolow(char s){//char小文字化
  return (s+0x20);
}
int ctoi(char a){//char->int
  if('0'<=a&&a<='9') return (a-'0');
  return -1;
}

int gcd(ll int a,ll int b) {//最大公約数
  if(a<b) return gcd(b,a);
  int c;
  while ((c=a%b)) {
    a=b;
    b=c;
  }
  return b;
}
int lcm(ll int a,ll int b){//最小公倍数
 return (a*b)/gcd(a,b);
}

int main(){
  int N;
  cin>>N;
  cout<<(N-2)*180;
}

// vector<vector<int>> 変数名(左の個数, vector<int>(右の個数));

//cout<<fixed<<setprecision(n);  //小数点n桁まで表示

//  vector<pair<int,int>> data(N);  //2つのデータを格納、.first/.secondで参照
// pairのソートはfirst優先の昇順


#include <bits/stdc++.h>

using namespace std;

typedef long long lol;
const int mod = 998244353;
inline int pls( int a , int b ) { a += b - mod; return a + ( a >> 31 & mod ); }
inline int mns( int a , int b ) { a -= b; return a + ( a >> 31 & mod ); }
inline void inc( int & a , int b ) { a += b - mod; a += a >> 31 & mod; }
inline void dec( int & a , int b ) { a -= b; a += a >> 31 & mod; }
inline int fpow( int base , int k ) {
  int res = 1;
  while( k ) {
    if( k & 1 ) res = (lol)res * base % mod;
    base = (lol)base * base % mod; k >>= 1;
  }
  return res;
} int _w;

const int N = 3e2 + 5;

int f[N][N][N] , n , k , sum[N] , m , ss[N];
char str[N];

int main( void ) {
  _w = scanf("%s%d",str+1,&k);
  n = strlen( str + 1 );
  str[n + 1] = '0';
  for( int i = 0 , r ; i <= n ; i = r ) {
    r = i + 1;
    while( r <= n && str[r] != '0' ) ++r;
    sum[++m] = r - i - 1;
  }
  for( int i = m ; i ; --i ) ss[i] = ss[i + 1] + sum[i];
  k = min( k , ss[1] );
  f[0][0][0] = 1;
  for( int i = 1 , s = 0 ; i <= m ; s += sum[i] , ++i )
    for( int j = s ; j <= ss[1] ; ++j )
      for( int l = 0 ; l <= k ; ++l ) if( f[i - 1][j][l] )
        for( int p = 0 ; p + j <= ss[1] && l + p - sum[i] <= k ; ++p ) {
          inc( f[i][j + p][l + max( p - sum[i] , 0 )] , f[i - 1][j][l] );
        }
  int ans = 0;
  for( int i = 0 ; i <= k ; ++i )
    inc( ans , f[m][ss[1]][i] );
  printf("%d",ans);
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
typedef long long ll;
typedef pair<ll,ll> pll;
typedef long double ld;
const ll inf=998244353;
const ll mod=998244353;
signed main(){
    string s;cin>>s;
    reverse(all(s));
    int n=s.size();
    int ma;cin>>ma;
    int cnt=0;
    vector<int>b(0);
    int la=0;
    rep(i,n){
        if(s[i]=='1')cnt++;
        else{
            b.pb(cnt-la);
            la=cnt;
        }
    }
    if(cnt==n){
        cout<<1<<endl;
        return 0;
    }
    ma=min(cnt,ma);
    int m=b.size();
    b.pb(cnt-la);
    vector<vector<vector<int> > > dp(m+1,vector<vector<int> >(ma+2,vector<int>(cnt+2)));
    dp[0][0][0]=1;
    rep(i,m){
        rep(j,ma+1){
            vector<ll>c(cnt+1);
            rep(k,cnt+1){
                c[k]+=dp[i][j][k];
                REP(l,1,b[i]+1){
                    if(j+l>ma)continue;
                    dp[i+1][j+l][k+l]+=dp[i][j][k];
                    dp[i+1][j+l][k+l]%=mod;
                }
                //cout<<dp[i][j][k]<<' ';
            }
            for(int k=cnt-1;k>=0;k--){
                c[k]+=c[k+1];
                c[k]%=mod;
            }
            rep(k,cnt+1){
                dp[i+1][j][k]+=c[k];
                dp[i+1][j][k]%=mod;
            }
            //cout<<endl;
        }
        //cout<<endl;
    }
    ll ans=0;
    rep(j,ma+1){
        rep(k,cnt+1){
            ans+=dp[m][j][k];
            ans%=mod;
        }
    }
    cout<<ans<<endl;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define mod 998244353
using namespace std;
typedef pair<llint, llint> P;
typedef pair<P, llint> E;

llint h, w, Q;
llint a[2005][2005];
llint sum[2005][2005], sumw[2005][2005], sumh[2005][2005];

llint get(llint a[2005][2005], llint sx, llint sy, llint tx, llint ty)
{
	return a[tx][ty] - a[sx-1][ty] - a[tx][sy-1] + a[sx-1][sy-1];
}

int main(void)
{
	cin >> h >> w >> Q;
	char c;
	for(int y = 1; y <= h; y++){
		for(int x = 1; x <= w; x++){
			cin >> c;
			a[x][y] = c%2;
		}
	}

	for(int y = 1; y <= h; y++){
		for(int x = 1; x <= w; x++){
			sum[x][y] = sum[x-1][y] + sum[x][y-1] - sum[x-1][y-1] + a[x][y];
			sumw[x][y] = sumw[x-1][y] + sumw[x][y-1] - sumw[x-1][y-1];
			if(a[x][y]*a[x-1][y]) sumw[x][y]++;
			sumh[x][y] = sumh[x-1][y] + sumh[x][y-1] - sumh[x-1][y-1];
			if(a[x][y]*a[x][y-1]) sumh[x][y]++;
		}
	}

	llint sx, sy, tx, ty;
	for(int q = 0; q < Q; q++){
		cin >> sy >> sx >> ty >> tx;
		llint ans = get(sum, sx, sy, tx, ty);
		ans -= get(sumw, sx+1, sy, tx, ty);
		ans -= get(sumh, sx, sy+1, tx, ty);
		cout << ans << endl;
	}

	return 0;
}

#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,K,Q,A,B;
string S;
//const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
const ll INF = 1LL << 60;
typedef pair<ll,ll> P;

int main(){
    cin>>N>>M>>Q;
    vector<string> grid(N);
    rep(i,N) cin>>grid[i];
    mat edge(N, vec(M, 0)), num(N + 1, vec(M + 1, 0)), addr(N, vec(M,0)), addc(N, vec(M,0));
    rep(i,N){
        rep(j,M){
            bool blue = grid[i][j] == '1';
            if(i > 0) {
                edge[i][j] += edge[i-1][j] + (blue && grid[i-1][j] == '1');
                addc[i][j] = addc[i-1][j] + (blue && grid[i-1][j] == '1');
            }
            if(j > 0) {
                edge[i][j] += edge[i][j-1] + (blue && grid[i][j-1] == '1');
                addr[i][j] = addr[i][j-1] + (blue && grid[i][j-1] == '1');
            }
            if(i > 0 && j > 0) edge[i][j] -= edge[i-1][j-1];
            num[i+1][j+1] = num[i][j+1] + num[i+1][j] - num[i][j] + blue;
        }
    }
    vec x(2), y(2);
    rep(i,Q){
        rep(j,2) cin>>x[j]>>y[j];
        --x[0], --y[0];
        ll res = 0;
        rep(j,2) rep(k,2) res += (j^k ? -1 : 1) * num[x[j]][y[k]];
        --x[1], --y[1];
        rep(j,2) rep(k,2) res += (j^k ? 1 : -1) * edge[x[j]][y[k]];
        res -= addr[x[0]][y[1]] - addr[x[0]][y[0]];
        res -= addc[x[1]][y[0]] - addc[x[0]][y[0]];
        cout<<res<<endl;
    }
    /*rep(i,N){
        rep(j,M) cout<<edge[i][j]<<' ';
        cout<<endl;
    }*/
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <functional>
#include <bitset>
#include <numeric>
#include <complex>
#include <iomanip>
#include <cassert>
#include <random>

#line 1 "lib/segment_tree.hpp"

#include <vector>
#include <algorithm>
#include <functional>

template<typename Monoid>
class SegmentTree {
public:
    using Operator = std::function<Monoid(Monoid, Monoid)>;

    SegmentTree(int n, const Operator &op, const Monoid init)
        : op(op), init(init) {

        num = 1;
        while (num < n) num *= 2;
        dat.resize(2 * num, init);
    }

    SegmentTree(const std::vector<Monoid> &m,
        const Operator &op, const Monoid init)
        : SegmentTree(m.size(), op, init) {

        int n = m.size();
        for (int i = 0; i < n; i++) {
            dat[num - 1 + i] = m[i];
        }
        for (int i = num - 2; i >= 0; i--) {
            dat[i] = op(dat[2 * i + 1], dat[2 * i + 2]);
        }
    }

    // Update the k-th value (0-indexed) to a.
    void update(int k, Monoid a) {
        k += num - 1;
        dat[k] = a;
        while (k > 0) {
            k = (k - 1) / 2;
            dat[k] = op(dat[2 * k + 1], dat[2 * k + 2]);
        }
    }

    // Get the value of the range [a, b).
    // k: The index of the node.
    // [left, right): The range corresponds to the k-th node.
    // Call like getval(a, b).
    Monoid getval(int a, int b, int k = 0, int left = 0, int right = -1) {
        if (right < 0) right = num;
        if (right <= a || b <= left) return init;
        if (a <= left && right <= b) return dat[k];
        Monoid vleft = getval(
            a, b, 2 * k + 1, left, left + (right - left) / 2);
        Monoid vright = getval(
            a, b, 2 * k + 2, left + (right - left) / 2, right);
        return op(vleft, vright);
    }

private:
    int num;
    std::vector<Monoid> dat;
    const Operator op;
    const Monoid init;
};

int main() {
    int n; std::cin >> n;
    using P = std::pair<long long, long long>;
    std::vector<P> lr(n);
    for (int i = 0; i < n; i++) {
        std::cin >> lr[i].first >> lr[i].second;
        lr[i].second++;
    }
    std::sort(lr.begin(), lr.end(), [](P &a, P &b) {
        return a.first == b.first ? a.second > b.second : a.first < b.first;
    });
    SegmentTree<P>
        seg(lr,
            [](const P &a, const P &b) -> P {
                return {
                    std::max(a.first, b.first),
                    std::min(a.second, b.second) };
            },
            { 0, 1e9 + 2 });
    long long ans = 0;
    for (int i = 0; i < n; i++) {
        long long v = lr[i].second - lr[i].first;
        auto p = seg.getval(0, i), q = seg.getval(i + 1, n);
        v += std::max(0LL,
            std::min(p.second, q.second) - std::max(p.first, q.first));
        ans = std::max(ans, v);
    }
    for (int i = 0; i < n - 1; i++) {
        auto p = seg.getval(0, i + 1), q = seg.getval(i + 1, n);
        auto u = std::max(0LL, p.second - p.first);
        auto v = std::max(0LL, q.second - q.first);
        ans = std::max(ans, u + v);
    }
    std::cout << ans << std::endl;
    return 0;
}

//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 100010001;
const ll LINF = (ll)INF*INF*10;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<P> lr(n);
    rep(i, n) cin >> lr[i];

    sort(all(lr));
    int ans = 0;
    vi mil(n+1, INT_MAX), mir(n+1, INT_MAX);
    rep(i, n) {
        mil[i+1] = min(mil[i], lr[i].second);
        mir[n-1-i] = min(mir[n-i], lr[n-1-i].second);
    }

    int min = INT_MAX;
    rep(i, n-1) {
        chmin(min, lr[i].second);
        if(min < lr[i].first) break;
        chmax(ans, min-lr[i].first + mir[i+1]-lr[n-1].first + 2);
    }
    rep(i, n) {
        chmax(ans, lr[i].second - lr[i].first + 1 + (mil[n] >= lr[n-1].first?mil[n]-lr[n-1].first+1:0));
    }

    cout << ans << endl;
}