#include <bits/stdc++.h>
using namespace std;
#define ll long long int
//#define f(i,a,n) for(ll i=a;i<n;i++)
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
ll isPrime(int n)
{
	// Corner cases
	if (n <= 1)
		return 1;
	if (n <= 3)
		return 2;

	// This is checked so that we can skip
	// middle five numbers in below loop
	if (n % 2 == 0 || n % 3 == 0)
		return 1;

	for (int i = 5; i * i <= n; i = i + 6)
		if (n % i == 0 || n % (i + 2) == 0)
			return 1;

	return 2;
}

ll fact(ll n){ll res = 1;for (ll i = 2; i <= n; i++)res = res * i,res%=1000000007;
    return res; }

int main()
{
    fast;
    ll t=1;
    //cin>>t;
    while(t--)
    {
        ll n,k,sum=0;
        cin>>n>>k;
        ll A[n];
        for(ll i=0;i<n;i++)
        {
            cin>>A[i];
        }
        sort(A,A+n);
        for(ll i=0;i<k;i++)
        {
            sum=sum+A[n-1-i];
        }
        cout<<sum;
    }
    return 0;
}

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

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const ll mod = 1e9+7;
const int maxn = 1e6 + 5;

void add(ll& x, ll y) {
    if (x>=mod) x%=mod;
    if (y>=mod) y%=mod;
    x += y;
    if (x>=mod) x%=mod;
}

int n, x;
int a[maxn];
const int N = 1e5;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);
    cin>>n>>x;
    for (int i=0; i<n; i++) {
	cin>>a[i];
    }
    sort(a,a+n);
    reverse(a,a+n);
    vector<ll> dp(N+10, 0);
    dp[x] = 1;

    for (int i=0; i<n; i++) {
	vector<ll> _dp(N+10);
	for (int x=0; x<=N; x++) {
	    if (dp[x]==0) continue;
	    // use a[i]
	    add(_dp[x%a[i]], dp[x]);
	    // don't use a[i]
	    add(_dp[x], 1ll*(n-i-1)*dp[x]%mod);
	}
	swap(_dp, dp);
    }

    ll res = 0;
    for (int x=0; x<=N; x++) {
	add(res, 1ll*x*dp[x]);
    }
    cout<<res<<endl;

    return 0;
}


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

int main(){
    int N, M;
    int tr[21][21];
    while(true){
        scanf("%d", &N);
        if(N == 0) break;
        for(int i = 0; i <= 20; i++){
            fill(tr[i], tr[i] + 21, 0);
        }
        for(int i = 0; i < N; i++){
            int x, y;
            scanf("%d%d", &x, &y);
            tr[x][y] = 1;
        }
        scanf("%d", &M);
        int cnt = 0;
        int rx = 10, ry = 10;
        for(int i = 0; i < M; i++){
            char d; int s;
            scanf(" %c%d", &d, &s);
            int dx, dy;
            if(d == 'N'){
                dx = 0, dy = 1;
            }else if(d == 'E'){
                dx = 1, dy = 0;
            }else if(d == 'S'){
                dx = 0, dy = -1;
            }else{
                dx = -1, dy = 0;
            }
            while(s--){
                rx += dx;
                ry += dy;
                if(tr[rx][ry] == 1){
                    tr[rx][ry] = 0;
                    cnt += 1;
                }
            }
        }
        if(cnt == N){
            puts("Yes");
        }else{
            puts("No");
        }
    }
    return 0;
}

#include <stdio.h>

typedef struct _Gem{
	int x;
	int y;
	bool hit;
} Gem;

static void
pick_gem(Gem* g, int n, int x, int y)
{
	for (int i = 0; i < n; i++) {
		if (!g[i].hit && g[i].x == x && g[i].y == y) {
			g[i].hit = true;
			return;
		}
	}
}

static bool
check_gem(Gem* g, int n)
{
	for (int i = 0; i < n; i++) {
		if (g[i].hit == false) {
			return false;
		}
	}

	return true;
}

int
main(void)
{

	char line[80];
	int n;
	int m;

	Gem g[20];

	char d[30];
	int l[30];

	while (true) {
		if (fgets(line, sizeof line, stdin) == NULL) {
			return 1;
		}
		if (sscanf(line, "%d", &n) != 1) {
			return 1;
		}

		if (n == 0) {
			break;
		}

		for (int i = 0; i < n; i++) {
			if (fgets(line, sizeof line, stdin) == NULL) {
				return 1;
			}
			if (sscanf(line, "%d %d", &(g[i].x), &(g[i].y)) != 2) {
				return 1;
			}
			g[i].hit = false;
		}

		if (fgets(line, sizeof line, stdin) == NULL) {
			return 1;
		}
		if (sscanf(line, "%d", &m) != 1) {
			return 1;
		}

		for (int i = 0; i < m; i++) {
			if (fgets(line, sizeof line, stdin) == NULL) {
				return 1;
			}
			if (sscanf(line, "%c %d", &(d[i]), &(l[i])) != 2) {
				return 1;
			}
		}

		int x = 10;
		int y = 10;
		for (int i = 0; i < m; i++) {
			if (d[i] == 'N') {
				for (int k = 1; k <= l[i]; k++) {
					pick_gem(g, n, x, ++y);
				}
			} else if (d[i] == 'S') {
				for (int k = 1; k <= l[i]; k++) {
					pick_gem(g, n, x, --y);
				}
			} else if (d[i] == 'E') {
				for (int k = 1; k <= l[i]; k++) {
					pick_gem(g, n, ++x, y);
				}
			} else {
				for (int k = 1; k <= l[i]; k++) {
					pick_gem(g, n, --x, y);
				}
			}
		}
		printf("%s\n", (check_gem(g, n) ? "Yes" : "No"));
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
ll div_2(ll a) {
  ll ans = 0;
  while(a % 2 == 0) {
    ans++;
    a /= 2;
  }
  return ans;
}
int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  ll min = 1000000000;
  rep(i, N) {
    cin >> A[i];
    if(min > div_2(A[i])) {
      min = div_2(A[i]);
    }
  }
  cout << min << endl;
}

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define Rep(i, n) for (int i = 0; i < (int)(n); i++)
#define BitRep(i, n) for (int i = 0; i < (int)(1 << n); i++)
#define All(vec) vec.begin(), vec.end()
#define Sum(vec) accumulate(vec.begin(), vec.end(), 0)
#define Sort(vec) sort(vec.begin(), vec.end())
#define Reverse(vec) reverse(vec.begin(), vec.end())
#define Next_permutation(vec) next_permutation(vec.begin(), vec.end())
#define PI acos(-1)

int main() {
    int N;
    cin >> N;
    vector<int> A(N);
    Rep(i, N) cin >> A[i];
    Sort(A);
    int index = A.size() - 1;
    ll e1 = 0, e2 = 0;
    while (index >= 1) {
        if (A[index] == A[index - 1]) {
            e1 = A[index];
            index -= 2;
            break;
        }
        index--;
    }
    while (index >= 1) {
        if (A[index] == A[index - 1]) {
            e2 = A[index];
            break;
        }
        index--;
    }
    cout << e1 * e2 << endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:108777216")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <bitset>
#include <queue>
#include <deque>
#include <utility>
#include <algorithm>
#include <ctime>
using namespace std;

int const MAX_N = 128;
int const MX_CNT = 2200000;

double x[MAX_N], y[MAX_N];
int cnt[MAX_N];

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

    int n;
    cin>>n;
    for (int i=0; i<n; i++) cin>>x[i]>>y[i];
    double R = 1e11;
    double ang_part = R / MX_CNT;
    int sum = 0;
    for (int i=0; i<MX_CNT; i++) {
        double xx = R*cos(ang_part*i);
        double yy = R*sin(ang_part*i);
        double mn_val = 1e150;
        int mn_ind = -1;
        for (int j=0; j<n; j++) {
            double val = (xx - x[j])*(xx - x[j]) + (yy - y[j])*(yy - y[j]);
            if (val < mn_val) {
                mn_val = val;
                mn_ind = j;
            }
        }
        cnt[mn_ind]++;
        sum++;
    }
    for (int i=0; i<n; i++) printf("%.6lf\n",cnt[i]/((double) sum));
   	return 0;
}

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

struct point{
    int x, y;
}w[110];
int n;
double P[210];
const double PI = acos(-1.0);

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> w[i].x >> w[i].y;
    }
    for(int i = 1; i <= n; i++)
    {
        int cnt = 0;
        for(int j = 1; j <= n; j++)
        {
            if(i == j) continue;
            P[cnt++] = atan2(w[j].y - w[i].y, w[j].x - w[i].x);
        }
        sort(P, P+cnt);
        for(int j = 0; j < cnt; j++) P[j + cnt] = P[j]+2* PI;
        double res = 0.0;
        for(int j = 0; j < cnt; j++) {
            res = max(res, PI - P[j + cnt - 1] + P[j]);
        }
        printf("%.10f\n", res/(2*PI));
    }
    return 0;
}


#include <iostream>
#include <cstdint>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cctype>
#include <cassert>
#include <climits>
#include <string>
#include <bitset>
#include <cfloat>
#include <unordered_set>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<ll>> vvll;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rrep(i, n) for (ll i = 1; i <= (n); ++i)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define mes(a) cout << (a) << endl
#define dmes(a, b) cout << (a) << " " << (b) << endl
#define re0 return 0
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define rSort(a) sort(a.rbegin(), a.rend())
#define Rev(a) reverse(a.begin(), a.end())
#define MATHPI acos(-1)
#define itn int
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return 1;
    }
    return 0;
}
struct io
{
    io()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
    }
};
const int INF = INT_MAX;
const ll LLINF = 1LL << 60;
const ll MOD = 1000000007;
const double EPS = 1e-19;

signed main() {
    ll n;
    cin >> n;

    string s;
    cin >> s;
    vi s2(n);

    ll r = 0;
    ll g = 0;
    ll b = 0;

    ll ans = 0;

    rep(i, n) {
        if (s[i] == 'R') {
            r++;
            s2[i] = 1;
        }
        if (s[i] == 'G') {
            g++;
            s2[i] = 2;
        }
        if (s[i] == 'B') {
            b++;
            s2[i] = 4;
        }
    }

    ans = r*g*b;

    rep(i, n) {
        for (int j = 1; j < n; j++) {
            if (i+j >= n || i+2*j >= n) continue;
            if (s2[i] + s2[i+j] + s2[i+2*j] == 7) ans--;
        }
    }

    mes(ans);

}


#include<iostream>
#include<string>
using namespace std;
string map[20]={""};
void check(int x,int y);
int main(void){
    while(1){
        int ans=0;
        for(int i=0;i<12;i++){
            cin>>map[i];
            if(cin.eof())goto end;
        }
        for(int i=0;i<12;i++){
            for(int j=0;j<12;j++){
                if(map[i][j]=='1'){
                    check(i,j);
                    ans++;
                }
            }
        }
        cout<<ans<<endl;
    }
    end:;
    return 0;
}
void check(int x,int y){
    map[x][y]='0';
    if(x!=11&&map[x+1][y]=='1')check(x+1,y);
    if(y!=11&&map[x][y+1]=='1')check(x,y+1);
    if(x!=0&&map[x-1][y]=='1')check(x-1,y);
    if(y!=0&&map[x][y-1]=='1')check(x,y-1);
    return;
}

#include <array>
#include <cstdlib>
#include <iostream>
#include <string>
#include <queue>
#include <vector>
class SSF {
private:
	void ScanLine(std::vector<std::string> *map, const char lx, const char rx, const char y, const char oy, std::array<std::queue<char>, 4> *buffer) {
		unsigned char i = lx;
		while (i <= rx) {
			while (i < rx) {
				if (map->at(y).at(i) != '0') {
					break;
				}
				++i;
			}
			if (map->at(y).at(i) == '0') {
				break;
			}
			buffer->at(0).push(i);
			while (i <= rx) {
				if (map->at(y).at(i) == '0') {
					break;
				}
				++i;
			}
			buffer->at(1).push(i - 1);
			buffer->at(2).push(y);
			buffer->at(3).push(oy);
		}
		return;
	}
public:
	unsigned char SeedFill(std::vector<std::string> *map, const unsigned char x, const unsigned char y) {
		std::array<std::queue<char>, 4> buffer;
		unsigned char lx, rx, ly, oy;
		if (map->at(y).at(x) == '0' || map->empty() == true) {
			return 0;
		}
		buffer.at(0).push(x);
		buffer.at(1).push(x);
		buffer.at(2).push(y);
		buffer.at(3).push(y);
		while (buffer.front().empty() == false) {
			char lxsav = buffer.at(0).front() - 1, rxsav = buffer.at(1).front() + 1;
			lx = buffer.at(0).front();
			buffer.at(0).pop();
			rx = buffer.at(1).front();
			buffer.at(1).pop();
			ly = buffer.at(2).front();
			buffer.at(2).pop();
			oy = buffer.at(3).front();
			buffer.at(3).pop();
			if (map->at(ly).at(lx) == '0') {
				continue;
			}
			while (rx < map->at(ly).size() - 1) {
				if (map->at(ly).at(rx + 1) == '0') {
					break;
				}
				++rx;
			}
			while (lx > 0) {
				if (map->at(ly).at(lx - 1) == '0') {
					break;
				}
				--lx;
			}
			for (unsigned char i = lx; i <= rx; ++i) {
				map->at(ly).at(i) = '0';
			}
			if (ly - 1 >= 0) {
				if (ly - 1 == oy) {
					ScanLine(map, lx, lxsav, ly - 1, ly, &buffer);
					ScanLine(map, rxsav, rx, ly - 1, ly, &buffer);
				}
				else {
					ScanLine(map, lx, rx, ly - 1, ly, &buffer);
				}
			}
			if (ly + 1 <= map->size() - 1) {
				if (ly + 1 == oy) {
					ScanLine(map, lx, lxsav, ly + 1, ly, &buffer);
					ScanLine(map, rxsav, rx, ly + 1, ly, &buffer);
				}
				else {
					ScanLine(map, lx, rx, ly + 1, ly, &buffer);
				}
			}
		}
		return 1;
	}
};
int main() {
	while (1) {
		SSF ssf;
		std::vector<std::string> map;
		unsigned short count = 0;
		do {
			map.resize(count + 1);
			std::getline(std::cin, map.at(count));
			if (map.at(count).empty() == true) {
				std::getline(std::cin, map.at(count));
				if (map.at(count).empty() == true) {
					return EXIT_SUCCESS;
				}
			}
			map.at(count).resize(map.front().size(), '0');
			map.at(count).shrink_to_fit();
		} while (++count < map.front().size());
		map.shrink_to_fit();
		count = 0;
		for (unsigned char j = 0; j < map.size(); ++j) {
			for (unsigned char i = 0; i < map.at(j).size(); ++i) {
				count += ssf.SeedFill(&map, i, j);
			}
		}
		std::cout << count << std::endl;
	}
	return EXIT_SUCCESS;
}


#include <iostream>
#include <cmath>

using namespace std;

int main() {

   double x[2]{}, y[2]{}, distance{};

   cin >> x[0] >> y[0] >> x[1] >> y[1];

   distance = sqrt(pow(x[0] - x[1], 2) + pow(y[0] - y[1], 2));
   cout << fixed << distance;

   return 0;

}

#include <bits/stdc++.h>
using namespace std;
int main() {
  int A, B, C;
  cin >> A >> B >> C;
  if (A == B && B == C) cout << "No" << endl;
  else if (A == B || B == C || C == A) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}


#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

size_t ldistance(string s, string t) {
    /* Levenshtein Distance */
    size_t slen=s.length(), tlen=t.length();
    vector<vector<size_t>> dp(slen+1, vector<size_t>(tlen+1));

    for (size_t i=0; i<=slen; ++i)
        dp[i][0] = i;

    for (size_t j=0; j<=tlen; ++j)
        dp[0][j] = j;

    for (size_t i=1; i<=slen; ++i)
        for (size_t j=1; j<=tlen; ++j) {
            size_t cost=(s[i-1]!=t[j-1]);
            dp[i][j] = min(min(dp[i-1][j], dp[i][j-1])+1, dp[i-1][j-1]+cost);
        }

    return dp[slen][tlen];
}

int main() {
    char buf[1024];
    scanf("%s", buf);
    string s1(buf);

    scanf("%s", buf);
    string s2(buf);

    printf("%zu\n", ldistance(s1, s2));
    return 0;
}

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<climits>
#include<ctime>
#include<cstring>
#include<stack>
#include<queue>
#include<sstream>
#include<string>
#include<set>
#include<array>
#include<cassert>

#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i);

using namespace std;

int dp[1001][1001];//from,to

const int INF=1<<28;

int main(){

	string from,to;
	cin >> from >> to;

	fill_n((int *)dp,sizeof(dp)/sizeof(int),0);
	rep(i,from.size()+1) dp[0][i]=i;
	rep(i,to.size()+1) dp[i][0]=i;

	for(int i=1;i<=to.size();i++){
		for(int j=1;j<=from.size();j++){
			if(to[i-1]==from[j-1]){
				dp[i][j] = min(dp[i-1][j] + 1, min(dp[i][j-1] + 1, dp[i-1][j-1] + 0));
			}else{
				dp[i][j] = min(dp[i-1][j] + 1, min(dp[i][j-1] + 1, dp[i-1][j-1] + 1));
			}
		}
	}

	cout << dp[to.size()][from.size()] << endl;

	return 0;
}


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

int main() {
  vector<int> x(3);
  int k, y;
  for(int i=0;i<3;i++) cin >> x.at(i);
  cin >> k;
  sort(x.begin(), x.end());
  y = x.at(2);
  for(int i=0;i<k;i++) y = 2*y;
  cout << x.at(0) + x.at(1) + y << endl;
}

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

int main() {
  int mod=1000000007;
  int n;
  cin >> n;
  long to=0;
  vector<int>a(n);
  vector<int>b(n);
  vector<int>c(n);
  rep(i,n){
    cin>>a.at(i)>>b.at(i);
    to-=b.at(i);
    c.at(i)=a.at(i)+b.at(i);
  }
  sort(c.begin(),c.end());
  reverse(c.begin(),c.end());
  rep(i,(n-1)/2+1){
    to+=c.at(i*2);
  }
  cout<<to<<endl;
}


#include<bits/stdc++.h>
#define F(i,a,b) for(int i=(a);i<=(b);++i)

int n,x[101],y[101];
const double PI=acos(-1);
double k[101];

int main(){
	scanf("%d",&n);
	if(n==2){puts("0.5\n0.5");return 0;}
	F(i,1,n) scanf("%d%d",x+i,y+i);
	F(i,1,n){
		int cnt=0;
		F(j,1,n) if(j!=i){
			k[++cnt]=atan2(x[j]-x[i],y[j]-y[i]);
		}
		std::sort(k+1,k+cnt+1);
		double t;
		t=k[1]-k[cnt]+PI+PI;
		if(t>=PI) {printf("%.15lf\n",(t-PI)/(PI+PI)); continue;}
		bool ok=0;
		F(j,1,cnt-1){
			t=k[j+1]-k[j];
			if(t>=PI) {printf("%.15lf\n",(t-PI)/(PI+PI)); ok=1; break;}
		} if(!ok) printf("%.15lf\n",0.);
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
const ll MOD = 1e9+7;
const ll MOD2 = 998244353;
const ll INF = LLONG_MAX;
const string alpha = "abcdefghijklmnopqrstuvwxyz";

//点a,bの距離の2乗
ld dist2(pdd a, pdd b){
    ld x = b.first - a.first;
    ld y = b.second - a.second;
    return x*x + y*y;
}

//点a,bの距離
ld dist(pdd a, pdd b){
    return sqrt(dist2(a, b));
}

//内積a・b
ld dot(pdd a, pdd b){
    ld x = a.first * b.first;
    ld y = a.second * b.second;
    return x+y;
}

//a,bを横に並べた行列の行列式
ld det(pdd a, pdd b){
    ld x = a.first * b.second;
    ld y = a.second * b.first;
    return x-y;
}

//b-a
pdd dif(pdd a, pdd b){
    pdd p;
    p.first = b.first - a.first;
    p.second = b.second - a.second;
    return p;
}

//点a,bの中点
pdd mid(pdd a, pdd b){
    pdd p;
    p.first = (a.first + b.first)/2;
    p.second = (a.second + b.second)/2;
    return p;
}

int main(){
    int N;
    cin >> N;
    pdd p[N];
    map<pdd, int> mp;
    rep(i, N){
        cin >> p[i].first >> p[i].second;
        mp[p[i]] = i;
    }
    sort(p, p+N);
    pdd q[N+1];
    int k = 1;
    q[0] = p[0];
    rep2(i, 1, N-1){
        if(i < N-1 && p[i-1].first == p[i].first) continue;
        while(k > 1 && det(dif(q[k-1], p[i]), dif(q[k-1], q[k-2])) <= 0) k--;
        q[k] = p[i], k++;
    }
    int t = k;
    rep3(i, N-2, 0){
        if(i > 0 && p[i+1].first == p[i].first) continue;
        while(k > t && det(dif(q[k-1], p[i]), dif(q[k-1], q[k-2])) <= 0) k--;
        q[k] = p[i], k++;
    }
    k--;
    ld ans[N];
    fill_n(ans, N, 0);
    ld pi = 3.141592653589793238;
    rep(i, k){
        int j = (k+i-1)%k, l = (i+1)%k;
        pdd x[2];
        x[0] = mid(q[i], q[j]);
        x[1] = mid(q[i], q[l]);
        ld r[3], R[3];
        rep(a, 2){
            r[a] = dist(q[i], x[a]);
            R[a] = dist2(q[i], x[a]);
        }
        r[2] = dist(x[0], x[1]);
        R[2] = dist2(x[0], x[1]);
        ld cos = -(R[0]+R[1]-R[2])/(2*r[0]*r[1]);
        if(cos > 1) cos = 1;
        elif(cos < -1) cos = -1;
        ld the = acos(cos);
        int n = mp[q[i]];
        ans[n] = the/(2*pi);
    }
    rep(i, N) cout << setprecision(15) << ans[i] << endl;
}


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

typedef long long llint;
typedef long double ld;
#define inf 1e18
#define mod 1000000007
priority_queue<llint,vector<llint>,greater<llint> > que;
priority_queue<llint> q;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

void solve(){
	llint n,a,b;
	cin >> n >> a >> b;
	vector<llint>x(n);
	for(int i=0;i<n;i++)cin >> x[i];
	llint ans=0;
	for(int i=1;i<n;i++){
		ans+=min((x[i]-x[i-1])*a,b);
	}
	cout << ans << endl;
}

int main(int argc, char *argv[]) {
  solve();
  return 0;
}


#include<cstdio>
int main(){
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        int point=0;
        int base=0;
        int out=0;
        while(out!=3){
            char s[8];
            scanf(" %s",s);
            switch(s[1]){
                case 'I':
                    base++;
                    if(base==4){
                        point++;
                        base--;
                    }
                    break;
                case 'U':
                    out++;
                    break;
                case 'O':
                    point+=base+1;
                    base=0;
                    break;
            }
        }
        printf("%d\n",point);
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n)for(int i=0;i<(n);i++)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
using ll = long long;
using P = pair<int, int>;

int main(){
    int n; cin >> n;
    vector<int> A;
    A.push_back(0);
    rep(i,n){
        int a; cin >> a;
        A.push_back(a);
    }
    A.push_back(0);
    int sum = 0;
    rep(i,n+1) sum += abs(A[i+1] - A[i]);
    for(int i = 1; i <= n; i++){
        int ans = sum;
        ans -= abs(A[i] - A[i-1]);
        ans -= abs(A[i+1] - A[i]);
        ans += abs(A[i+1] - A[i-1]);
        cout << ans << endl;
    }
}


#include <bits/stdc++.h>
#define assem99 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define ld long double
#define endll '\n'
#define sz(a) (int)(a).size()
#define RWFile freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
int dx[] = { 0, -1, 0, 1, -1, 1, -1, 1 };
int dy[] = { -1, 0, 1, 0, 1, -1, -1, 1 };
const int MOD = 1e9 + 7, N = 2e6 + 5, oo = 1e9;
const double pi = acos(-1);
using namespace std;

int main()
{
    assem99
    int n, m; cin>>n>>m;
    ll ans = 0, cnt = 1, sum = 0;
    map<int, int> freq;
    freq[0] = 1;
    for(int i = 0; i < n; i++)
    {
        ll a; cin>>a;
        sum += a;
        sum %= m;
        ans += freq[sum]++;
    }
    cout<<ans;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> P;
int INF = 1e18;
int mod = 1e9+7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
signed main() {
    int N,M;
    cin >> N >> M;
    vector<int>A(N+1);
    for(int i = 1; i <= N; i++) {
        cin >> A[i];
        A[i] += A[i-1];
        A[i]%=M;
    }
    sort(A.begin(),A.end());
    int ans = 0;
    int cnt = 1;
    for(int i = 1; i <= N; i++) {
        if(A[i] == A[i-1]) {
            cnt++;
        }
        else {
            ans += cnt*(cnt-1)/2;
            cnt = 1;
        }
    }
    ans += cnt*(cnt-1)/2;
    cout << ans << endl;
}


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

const int MAX_N = 303;
const int MAX_A = 303;
const ll MOD = 998244353;

ll A[MAX_N], dp[MAX_N][MAX_N * MAX_A], sub_dp[MAX_N][MAX_N * MAX_A];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    ll sum = 0;
    for (int i = 1; i <= N; i++) {
        cin >> A[i];
        sum += A[i];
    }
    // main
    dp[0][0] = 1;
    for (int i = 1; i <= N; i++) {
        for (int j = 0; j <= sum; j++) {
            dp[i][j] = (dp[i][j] + (dp[i - 1][j] * 2) % MOD) % MOD;
            if (j - A[i] >= 0) dp[i][j] = (dp[i][j] + dp[i - 1][j - A[i]]) % MOD;
        }
    }
    // sub
    sub_dp[0][0] = 1;
    for (int i = 1; i <= N; i++) {
        for (int j = 0; j <= sum; j++) {
            sub_dp[i][j] = (sub_dp[i][j] + sub_dp[i - 1][j]) % MOD;
            if (j - A[i] >= 0) sub_dp[i][j] = (sub_dp[i][j] + sub_dp[i - 1][j - A[i]]) % MOD;
        }
    }
    ll not_ans = (MOD - (sum % 2 == 0 ? sub_dp[N][sum / 2] : 0)) % MOD;
    for (int i = (sum + 1) / 2; i <= sum; i++) {
        not_ans = (not_ans + dp[N][i]) % MOD;
    }
    ll ans = 1;
    for (int i = 0; i < N; i++) ans = (ans * 3) % MOD;
    cout << (ans - (3 * not_ans) % MOD + MOD) % MOD << endl;
    return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int mod=998244353;
#define N 321
inline int read(){
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=(x<<1)+(x<<3)+c-'0';
		c=getchar();
	}
	return x*f;
}
int n,a[N],dp[N*N],sum,ans;
int main(){
	n=read();
	ans=1;
	for(int i=1;i<=n;++i){
		a[i]=read();
		ans=3LL*ans%mod;
	}
	dp[0]=1;
	for(int i=1;i<=n;++i){
		sum+=a[i];
		for(int j=sum;j>=0;--j){
			(dp[j]<<=1)%=mod;
			if(j>=a[i])dp[j]=(dp[j]+dp[j-a[i]])%mod;
		}
	}
	for(int i=sum;sum-i<=i;--i){
		ans=(ans-3LL*dp[i]%mod+mod)%mod;
	}
	if(!(sum&1)){
		memset(dp,0,sizeof(dp));
		dp[0]=1;
		for(int i=1;i<=n;++i){
			for(int j=sum;j>=a[i];--j){
				dp[j]=(dp[j]+dp[j-a[i]])%mod;
			}
		}
		ans=(ans+3LL*dp[sum>>1])%mod;
	}
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rrep(i,n) for(ll i = 1; i <= (n); ++i)
#define drep(i,n) for(ll i = (n)-1; i >= 0; --i)
#define all(v) v.begin(),v.end()
#define len(x) (ll)(x).length()
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define pb push_back
#define sz(x) (ll)(x).size()
#define v(T) vector<T>
#define vv(T) vector<vector<T>>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
ll gcd(ll a,ll b){if(a%b==0){return b;}else{return(gcd(b,a%b));}}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
const int INF=1e9;
const ll MX = 1e18;
const ll MOD=INF+7;
const int di[] = {-1,0,1,0};
const int dj[] = {0,-1,0,1};
const double PI=acos(-1);
const string tr="abcdefghijklmnopqrstuvwxyz";
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
#define YN {puts("YES");}else{puts("NO");}
const ll MAX = 1000001;
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {fac[0] = fac[1] = 1;finv[0] = finv[1] = 1;inv[1] = 1;for (ll i = 2; i < MAX; i++){fac[i] = fac[i - 1] * i % MOD;inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;finv[i] = finv[i - 1] * inv[i] % MOD;}}
long long COM(ll n, ll k){if (n < k) return 0;if (n < 0 || k < 0) return 0;return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;}
ll llpow(ll n,ll i){ll cnt=n;  for(ll j=0;j<i-1;j++){n*=cnt;}return n;}
bool is_prime(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;}
int digit(ll N) {int ans = 0;while (N) {++ans;N /= 10;}return ans;}

int main() {
  ll N;
  cin >> N;
  ll res = MX;
  for (ll A = 1; A * A <= N; ++A) {
    if (N % A == 0) {
      ll B = N / A;
      ll cnt = max(digit(A),digit(B));
      res = min(res, cnt);
    }
  }
  cout << res << endl;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
constexpr int INF = 0x3f3f3f3f; constexpr ll LLINF = 0x3f3f3f3f3f3f3f3f;
#define db(x) { cerr << #x << " = " << x << endl; }
template <typename T> void _dbarr(T* a, size_t sz){ for(int i = 0; i < sz; i++) cerr << a[i] << " \n"[i == sz-1]; }
template <typename T> void _dbarr(vector<T> a, size_t sz){ for(int i = 0; i < sz; i++) cerr << a[i] << " \n"[i == sz-1]; }
#define dbarr(x, n) {cerr << #x << ": "; _dbarr((x),(n));}
#define all(x) begin(x), end(x)
#define pb push_back
#define eb emplace_back
#define mpr make_pair
#define fs first
#define sn second

int main(){
	cin.tie(0)->sync_with_stdio(0);
	//freopen("in.txt","r", stdin);
    int a;
    cin >> a;
    cout << a + a*a + a*a*a << "\n";
}


#include<bits/stdc++.h>

int main(){
    using namespace std;
    unsigned long N, M;
    cin >> N >> M;
    vector<unsigned long> uf(2 * N), sz(2 * N);
    iota(uf.begin(), uf.end(), 0UL);
    fill(sz.begin(), sz.begin() + N, 1);
    const auto& find = [&uf](unsigned long i) -> unsigned long {
        while(i != uf[i])i = uf[i] = uf[uf[i]];
        return i;
    };
    const auto& unite = [&uf, &sz, &find](unsigned long i, unsigned long j) -> unsigned long {
        i = find(i);
        j = find(j);
        if(i == j)return 0;
        if(sz[i] < sz[j])swap(i, j);
        sz[i] += sz[j];
        uf[j] = i;
        return 1;
    };
    const auto& add_edge = [&N, &unite](unsigned long i, unsigned long j) -> void { unite(i, j + N); unite(i + N, j); };
    for(unsigned long i{0}, u, v; i < M; ++i){
        cin >> u >> v;
        add_edge(u - 1, v - 1);
    }
    if(find(0) == find(N))cout << N * (N - 1) / 2 - M << endl;
    else cout << sz[find(0)] * sz[find(N)] - M << endl;
    return 0;
}

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define rreps(i, n) for (int i = int(n); i >= 1; i--)
#define repc(i, n) for (int i = 0; i <= int(n); i++)
#define rrepc(i, n) for (int i = int(n); i >= 0; i--)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define repic(i, a, b) for (int i = int(a); i <= int(b); i++)
#define each(x, y) for (auto &x : y)
#define all(a) (a).begin(), (a).end()
#define bit32(x) (1 << (x))
#define bit64(x) (1ll << (x))
#define sz(v) ((int) v.size())

using namespace std;

using i64 = long long;
using f80 = long double;
using vi32 = vector<int>;
using vi64 = vector<i64>;
using vf80 = vector<f80>;
using vstr = vector<string>;

inline void yes() { cout << "Yes" << endl; exit(0); }
inline void no() { cout << "No" << endl; exit(0); }
inline i64 gcd(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); if (a % b == 0) return b; return gcd(b, a % b); }
inline i64 lcm(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); return a / gcd(a, b) * b; }
template <typename T> class pqasc : public priority_queue<T, vector<T>, greater<T>> {};
template <typename T> class pqdesc : public priority_queue<T, vector<T>, less<T>> {};
template <typename T> inline void amax(T &x, T y) { x = max(x, y); }
template <typename T> inline void amin(T &x, T y) { x = min(x, y); }
template <typename T> inline T exp(T x, i64 n, T e = 1) { T r = e; while (n > 0) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; }
template <typename T> istream& operator>>(istream &is, vector<T> &v) { each(x, v) is >> x; return is; }
template <typename T> ostream& operator<<(ostream &os, vector<T> &v) { rep(i, v.size()) { if (i) os << ' '; os << v[i]; } return os; }
void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(16); solve(); return 0; }

void solve() {
  int n, m;
  cin >> n >> m;
  vector<vi32> g(n);
  rep(i, m) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[u].emplace_back(v);
    g[v].emplace_back(u);
  }
  vi32 dist(n, 1e9);
  auto dfs = [&](auto dfs, int u, int d) -> bool {
    if (dist[u] != 1e9) {
      return dist[u] % 2 == d % 2;
    }
    dist[u] = d;
    for (auto v : g[u]) {
      if (!dfs(dfs, v, d + 1)) return false;
    }
    return true;
  };
  if (dfs(dfs, 0, 0)) {
    int a = 0;
    rep(i, n) a += dist[i] % 2 == 0;
    int b = n - a;
    cout << (i64) a * b - m << endl;
  } else {
    cout << (i64) n * (n - 1) / 2 - m << endl;
  }
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 240000
typedef long long LL;
const LL mod = 1000000007;
LL A[maxn], D;
LL mi[2][maxn * 4];
void build(int p, int o, int l, int r){
	if(l == r){
		mi[p][o] = p ? D * l + A[l] : - D * l + A[l];
		return;
	}
	int m = (l + r) >> 1;
	build(p, o * 2, l, m);
	build(p, o * 2 + 1, m + 1, r);
	mi[p][o] = min(mi[p][o * 2], mi[p][o * 2 + 1]);
}
LL ask(int p, int o, int l, int r, int L, int R){
	if(l >= L && r <= R) return mi[p][o];
	int m = (l + r) >> 1;
	LL ret = 1E18;
	if(L <= m) ret = min(ret, ask(p, o * 2, l, m, L, R));
	if(R > m) ret = min(ret, ask(p, o * 2 + 1, m + 1, r, L, R));
	return ret;
}
void update(int p, int o, int l, int r, int i){
	if(l == r) mi[p][o] = 1E18;
	else{
		int m = (l + r) >> 1;
		if(i <= m) update(p, o * 2, l, m, i);
		else update(p, o * 2 + 1, m + 1, r, i);
		mi[p][o] = min(mi[p][o * 2], mi[p][o * 2 + 1]);
	}
}
vector<pair<LL, int> > v;
int main(){
	int N;
	cin >> N >> D;
	LL ans = D * (N - 1);
	for(int i = 1; i <= N; i += 1){
		cin >> A[i];
		ans += A[i] << 1;
	}
	ans -= A[1] + A[N];
	for(int i = 2; i < N; i += 1) v.push_back(make_pair(A[i], i));
	sort(v.begin(), v.end());
	build(0, 1, 1, N);
	build(1, 1, 1, N);
	for(auto p : v){
		int i = p.second;
		LL L = ask(0, 1, 1, N, 1, i - 1) + D * i - A[i];
		LL R = ask(1, 1, 1, N, i + 1, N) - D * i - A[i];
		if(L >= 0 && R >= 0) continue;
		ans += min(L, R);
		update(0, 1, 1, N, i);
		update(1, 1, 1, N, i);
	}
	cout << ans;
}

#include<iostream>
#include<algorithm>
#include<functional>
#include<vector>
#include<queue>
using namespace std;
#define LL long long
#define LP pair<LL, LL>
const LL inf = 1e17;
struct segtree {
	int n = 1;
	vector<LP> node;//cost, place
	void init(int N) {
		node.clear();
		while (N > n) n *= 2;
		node.resize(n * 2, { inf, inf });
	}

	void update(int k, LL x, int a) {

		k += n - 1;
		node[k] = { x, a };
		while (k > 0) {
			k = (k - 1) / 2;
			int l = k * 2 + 1;
			int r = k * 2 + 2;
			if (node[l].first < node[r].first) node[k] = node[l];
			else node[k] = node[r];
		}
	}

	LP mini(int a, int b, int k = 0, int l = 0, int r = -1) {

		if (r < 0) r = n;
		if (b <= l || r <= a) return { inf, inf };
		if (a <= l && r <= b) return node[k];
		LP ln = mini(a, b, k * 2 + 1, l, (l + r) / 2);
		LP rn = mini(a, b, k * 2 + 2, (l + r) / 2, r);
		if (ln.first < rn.first) return ln;
		else return rn;
	}
};

struct edge {
	LL to;
	LL cost;
	bool operator<(const edge &e1) const {
		return e1.cost < cost;
	}
};

int N;
int idx[200010];
LL D;
bool used[200010];
vector<LL> A;
vector<LP> B;
vector<edge> G[200010];

LL prim() {
	priority_queue<edge> pq;
	pq.push(edge{ 0, 0 });
	LL total = 0;
	while (!pq.empty()) {
		edge e = pq.top();
		pq.pop();
		if (!used[e.to]) {
			used[e.to] = true;
			total += e.cost;
			for (auto u : G[e.to]) {
				if (!used[u.to]) pq.push(u);
			}
		}
	}
	return total;
}

void build() {
	segtree seg;
	seg.init(N);

	for (int i = 0; i < N; i++) {
		seg.update(idx[i], A[i] + D * (N - i - 1), idx[i]);
		if (i != 0) {
			LP lp = seg.mini(0, idx[i]);
			if (lp.first == inf) continue;

			lp.first -= D * (N - i - 1) - A[i];
			G[B[lp.second].second].push_back(edge{ i, lp.first });
			G[i].push_back(edge{ B[lp.second].second, lp.first });
		}
	}

	seg.init(N);
	for (int i = N - 1; i >= 0; i--) {
		seg.update(idx[i], A[i] + D * i, idx[i]);
		if (i != N - 1) {
			LP lp = seg.mini(0, idx[i]);
			if (lp.first == inf) continue;

			lp.first -= D * i - A[i];
			G[B[lp.second].second].push_back(edge{ i, lp.first });
			G[i].push_back(edge{ B[lp.second].second, lp.first });
		}
	}
}

void init() {
	cin >> N >> D;
	for (int i = 0; i < N; i++) {
		LP tmp;
		cin >> tmp.first;
		tmp.second = i;
		A.push_back(tmp.first);
		B.push_back(tmp);
	}
	sort(B.begin(), B.end());

	for (int i = 0; i < N; i++) {
		idx[B[i].second] = i;
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	init();
	build();
	cout << prim() << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define il inline
#define gc getchar()
#define ll long long
#define int long long
#define ri register int
#define rc register char
#define rb register bool
#define rp(i,x,y) for(ri i=x;i<=y;++i)
#define my(i,x,y) for(ri i=x;i>=y;--i)

const int N=200000+10,inf=1e18;
int n,q,pre,nw,tag[N<<2];
ll tr1[N<<2],tr2[N<<2];

il int read()
{
    rc ch=gc;ri x=0;rb y=1;
    while(ch!='-' && (ch>'9' || ch<'0'))ch=gc;
    if(ch=='-')ch=gc,y=0;
    while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^'0'),ch=gc;
    return y?x:-x;
}
void pushdown(ri nw)
{
	if(!tag[nw])return;
	tag[nw<<1]+=tag[nw];tag[nw<<1|1]+=tag[nw];
	tr1[nw<<1]+=tag[nw];tr1[nw<<1|1]+=tag[nw];
	tr2[nw<<1]+=tag[nw];tr2[nw<<1|1]+=tag[nw];
	tag[nw]=0;
}
void modify(ri nw,ri l,ri r,ri to,ll dat)
{
	if(l==r){tr1[nw]=min(tr1[nw],dat-to),tr2[nw]=min(tr2[nw],dat+to);return;}
	pushdown(nw);ri mid=(l+r)>>1;to<=mid?modify(nw<<1,l,mid,to,dat):modify(nw<<1|1,mid+1,r,to,dat);
	tr1[nw]=min(tr1[nw<<1],tr1[nw<<1|1]);tr2[nw]=min(tr2[nw<<1],tr2[nw<<1|1]);
}
ll query1(ri nw,ri l,ri r,ri to_l,ri to_r)
{
	if(to_l<=l && r<=to_r)return tr1[nw];
	pushdown(nw);ri mid=(l+r)>>1;ll ret=inf;
	if(to_l<=mid)ret=query1(nw<<1,l,mid,to_l,to_r);
	if(mid<to_r)ret=min(ret,query1(nw<<1|1,mid+1,r,to_l,to_r));
	return ret;
}
ll query2(ri nw,ri l,ri r,ri to_l,ri to_r)
{
	if(to_l<=l && r<=to_r)return tr2[nw];
	pushdown(nw);ri mid=(l+r)>>1;ll ret=inf;
	if(to_l<=mid)ret=query2(nw<<1,l,mid,to_l,to_r);
	if(mid<to_r)ret=min(ret,query2(nw<<1|1,mid+1,r,to_l,to_r));
	return ret;
}
ll query(ri nw,ri l,ri r)
{
	if(l==r)return tr1[nw]+l;
	pushdown(nw);ri mid=(l+r)>>1;return min(query(nw<<1,l,mid),query(nw<<1|1,mid+1,r));
}

signed main()
{
//	freopen("2558.in","r",stdin);//freopen("2558.out","w",stdout);
	n=read();q=read();pre=read();memset(tr1,63,sizeof(tr1));memset(tr2,63,sizeof(tr2));modify(1,1,n,read(),0);
	while(q--)
	{
		nw=read();ll dat=min(query1(1,1,n,1,nw)+nw,query2(1,1,n,nw,n)-nw);
		tag[1]+=abs(nw-pre);tr1[1]+=abs(nw-pre);tr2[1]+=abs(nw-pre);modify(1,1,n,pre,dat);pre=nw;
	}
	printf("%lld\n",query(1,1,n));
	return 0;
}

#include <bits/stdc++.h>

using namespace std;
#define SIZE 262144
#define int long long
#define INF 1000000000000000000LL
class segtree{
public:
	int seg[SIZE * 2];
	void update(int a, int b){
		a += SIZE;
		seg[a] = b;
		while(true){
			a /= 2;
			if (a == 0)break;
			seg[a] = min(seg[a * 2], seg[a * 2 + 1]);
		}
	}
	int query(int beg, int end, int node, int lb, int ub){
		if (ub < beg || end < lb)return INF;
		if (beg <= lb&&ub <= end)return seg[node];
		return min(query(beg, end, node * 2, lb, (lb + ub) / 2), query(beg, end, node * 2 + 1, (lb + ub) / 2 + 1, ub));
	}
};
int dp[SIZE];
segtree tree1, tree2;
signed main(){
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	int n, q, a, b;
	cin >> n >> q >> a >> b;
	a--, b--;
	int curb = b;
	fill(dp, dp + n, INF);
	dp[a] = 0;
	for (int i = 0; i < n; i++){
		tree1.update(i, dp[i] + i);
	}
	for (int i = 0; i < n; i++){
		tree2.update(i, dp[i] - i);
	}
	int dif = 0;
	for (int p = 0; p < q; p++){
		int z;
		cin >> z;
		z--;
		int t1 = tree2.query(0, z, 1, 0, SIZE - 1) + dif + z;
		int t2 = tree1.query(z, n - 1, 1, 0, SIZE - 1) + dif - z;
		dif += abs(z - curb);
		if (dp[curb] + dif > min(t1, t2)){
			dp[curb] = min(t1, t2) - dif;
			tree1.update(curb, dp[curb] + curb);
			tree2.update(curb, dp[curb] - curb);
		}
		curb = z;
	}
	int mn = INF;
	for (int i = 0; i < n; i++){
		mn = min(mn, dp[i] + dif);
	}
	cout << mn;
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ull unsigned long long
#define db long double
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define mp make_pair
#define all(x) (x).begin(), (x).end()

void dout() { cerr << '\n'; }

template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
    cerr << " " << H;
    dout(T...);
}

#ifdef LOCAL
    #define dbg(...) cerr << #__VA_ARGS__, dout(__VA_ARGS__)
#else
    #define dbg(...) ;
#endif

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair <int, int> pii;

const int N = 2007;
int n, m, adj[N][N], good[N][N];
int f[N][N], l[N][N], r[N][N];

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

    cin >> n >> m;
    char ch;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> ch;
            if (ch == '.') {
                adj[i][j] = 1;
            }
        }
    }
    for (int i = 1; i < n; i++) {
        for (int j = 1; j < m; j++) {
            good[i][j] = 1;
            if (adj[i][j] ^ adj[i][j + 1] ^ adj[i + 1][j] ^ adj[i + 1][j + 1]) {
                good[i][j] = 0;
            }
        }
    }
    for (int j = 1; j < m; j++) {
        for (int i = 1; i < n; i++) {
            if (good[i][j]) {
                f[i][j] = f[i - 1][j] + 1;
            }
        }
    }
    stack <int> st;
    int ans = max(n, m);
    for (int i = 1; i < n; i++) {
        for (int j = 1; j < m; j++) {
            while (!st.empty() && f[i][st.top()] >= f[i][j]) {
                st.pop();
            }
            if (st.empty()) {
                l[i][j] = 1;
            }   else {
                l[i][j] = st.top() + 1;
            }
            st.push(j);
        }
        while (!st.empty()) {
            st.pop();
        }
        for (int j = m - 1; j >= 1; j--) {
            while (!st.empty() && f[i][st.top()] >= f[i][j]) {
                st.pop();
            }
            if (st.empty()) {
                r[i][j] = m - 1;
            }   else {
                r[i][j] = st.top() - 1;
            }
            st.push(j);

            ans = max(ans, (r[i][j] - l[i][j] + 2) * (f[i][j] + 1));
        }
        while (!st.empty()) {
            st.pop();
        }
    }
    cout << ans;
}

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

using namespace std;

int main() {
	int n;
	long long int m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) {
			break;
		}
		long long int b;
		vector<long long int> ss;
		ss.push_back(0);
		for (int i = 0; i < n; i++) {
			cin >> b;
			if (b <= m) {
				ss.push_back(b);
			}
		}
		vector<long long int> ss2;
		for (long long int i = 0; i < ss.size(); i++) {
			ss2.push_back(ss[i]);
			for (long long int j = i; j < ss.size(); j++) {
				if (ss[i]+ss[j] <= m) {
					ss2.push_back(ss[i]+ss[j]);
				}
			}
		}
		sort(ss2.begin(), ss2.end());
		/*for (long long int i = 0; i < ss2.size(); i++) {
			cout << ss2[i] << endl;
		}*/
		long long int MAX = 0;
		long long int sz = ss2.size();
		for (long long int j = 0; j < sz; j++) {
			long long int a = m;
			for (long long int i = 0; i < 2; i++) {
				vector<long long int>::iterator ite = upper_bound(ss2.begin(), ss2.end(), a);
				if (ite != ss.begin()) {
					ite--;
					a -= *(ite);
				}
			}
			vector<long long int>::iterator ite2 = ss2.end();
			ss2.erase(ite2-1);
			MAX = max(MAX, m-a);
		}
		cout << MAX << endl;
	}
	return 0;
}


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

int main() {
  ll a,b;
  cin >> a  >> b;

  vector<ll> ay;
  vector<ll> by;

  for(ll i = 1; i <= sqrt(a);i++){
    if(a%i == 0){
      ay.push_back(i);
      if(i != a/i){
        ay.push_back(a/i);
      }
    }
  }
  for(ll i = 1; i <= sqrt(b);i++){
    if(b%i == 0){
      by.push_back(i);
      if(i != b/i){
        by.push_back(b/i);
      }
    }
  }
  //cout << ay.size() << " " << by.size() << endl;
  vector<ll> kys;
  for(int i = 0;i<ay.size();i++){
    for(int j = 0;j<by.size();j++){

      if(ay[i]==by[j]){
        kys.push_back(ay[i]);
        //cout << ay[i] << " " << by[j]<< endl;
      }
    }
  }

  int res = 0;
  int flg = 0;
  sort(kys.begin(),kys.end(),greater<ll>());
  for(int i = 0;i<kys.size();i++){
    flg = 0;
    for(int j = i+1;j<kys.size();j++){
      if(__gcd(kys[i],kys[j]) != 1){
        flg = 1;
        break;
      }
    }
    if(flg == 0){res++;}
  }

  cout << res;
}

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

using namespace std;

#define ll long long
#define rep(i, n) for(int i = 0; i < n; i++)
#define P pair<int, int>

typedef vector<int> vec;
typedef vector<vec> mat;

const ll mod = 1000000007;

vector<pair<ll, ll> > prime;

void prime_fuct(ll x){
    for (ll i = 2; i * i <= x; i++){
        if(x % i == 0){
            pair<ll, ll> p = make_pair(i, 0);
            while (x % i == 0){
                x /= i;
                p.second++;
            }
            prime.push_back(p);
        }
    }
    if(x > 1){
        pair<ll, ll> p = make_pair(x, 1);
        prime.push_back(p);
    }
}

ll gcd(ll  x, ll y){
    if(y == 0) return x;
    else  return gcd(y, x % y);
}

int main(){
    ll a, b;
    cin >> a >> b;
    ll n = gcd(a, b);
    prime_fuct(n);
    ll ans = (ll)prime.size() + 1;
    cout << ans << endl;
}


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

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

    cout << n * m << endl;
}

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

#define _USE_MATH_DEFINES
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

void abc_160a();

void abc_161a();

void abc_162a();

void abc_163a();

void abc_164a();

void abc_165a();

void abc_166a();

void abc_167a();

void abc_168a();

void abc_169();

int main()
{
  //abc_160a();
  //abc_161a();
  //abc_162a();
  //abc_163a();
  //abc_164a();
  //abc_165a();
  //abc_166a();
  //abc_167a();
  //abc_168a();
  abc_169();

}

void abc_160a()
{
  string S;
  cin >> S;

  if (S[2] == S[3] && S[4] == S[5]) cout << "Yes\n";
  else cout << "No\n";
}

void swap(int& a, int& b)
{
  int tmp;
  tmp = a;
  a = b;
  b = tmp;
}

void abc_161a()
{
  int x, y, z;
  cin >> x >> y >> z;

  swap(x, y);
  swap(x, z);

  cout << x << " " << y << " " << z << "\n";
}

void abc_162a()
{
  string s;
  cin >> s;
  for (char c : s)
  {
    if (c == '7')
    {
      cout << "Yes\n";
      return;
    }
  }

  cout << "No\n";
}

void abc_163a()
{
  int R;
  cin >> R;

  cout << (double)(2 * R * M_PI);
}

void abc_164a()
{
  int s, w;
  cin >> s >> w;

  if (s <= w) cout << "unsafe\n";
  else cout << "safe\n";
}

void abc_165a()
{
  int k, a, b;
  cin >> k >> a >> b;

  for (int i = a; i <= b; ++i)
  {
    if (i % k == 0)
    {
      cout << "OK\n";
      return;
    }
  }

  cout << "NG\n";
}

void abc_166a()
{
  string s;
  cin >> s;

  if (s == "ABC") cout << "ARC\n";
  if (s == "ARC") cout << "ABC\n";

}

void abc_167a()
{
  string s, t;
  cin >> s >> t;

  for (int i = 0; i < s.size(); ++i)
  {
    if (s[i] != t[i])
    {
      cout << "No\n";
      return;
    }
  }

  if ('a' <= t[t.size() - 1] && t[t.size() - 1] <= 'z') cout << "Yes\n";
  else cout << "No\n";

}

void abc_168a()
{
  string s;
  cin >> s;

  if (s[s.size() - 1] == '3') cout << "bon\n";
  else if (s[s.size() - 1] == '0' ||
    s[s.size() - 1] == '1' ||
    s[s.size() - 1] == '6' ||
    s[s.size() - 1] == '8') cout << "pon\n";
  else cout << "hon\n";
}

void abc_169()
{
  int a, b;
  cin >> a >> b;
  cout << a * b << "\n";
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e15;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
// __uint128_t
const ll MOD = 1e9+7;

int main(){
    ll H, W; cin >> H >> W;
    vector<string> S(H); for(int i = 0; i < H; i++) cin >> S[i];
    ll bs = 0;
    for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++) if(S[i][j] == '#') bs++;
    }

    vector<vector<ll>> V(H, vector<ll>(W, INF));
    queue<ll> qh, qw;
    qh.push(0); qw.push(0);
    V[0][0] = 0;
    int dh[] = {0, 1, 0, -1};
    int dw[] = {1, 0, -1, 0};

    while(!qh.empty()){
        ll h = qh.front(), w = qw.front();
        qh.pop(), qw.pop();
        for(int i = 0; i < 4; i++){
            ll nh = h+dh[i], nw = w+dw[i];
            if(0<=nh && nh < H && 0<=nw && nw<W && S[nh][nw] == '.' && V[h][w]+1<V[nh][nw]){
                qh.push(nh);
                qw.push(nw);
                V[nh][nw] = V[h][w]+1;
            }
        }
    }
    if(V[H-1][W-1] == INF) cout << -1 << endl;
    else cout << H*W-bs-V[H-1][W-1]-1 << endl;

}

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

#pragma region
using namespace std;
#define FOR(i,r,n) for(ll i = (ll)(r); i < (ll)(n); i++)
#define rep(i,n) FOR(i,0LL,n)
#define RFOR(i,r,n) for(ll i=(ll)(n-1);i>=r;i--)
#define rrep(i,n) RFOR(i,0LL,n)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define COUNT(a,y,x) upper_bound(all(a), y) - lower_bound(all(a), x)
#define UNIQUE(a) sort(all(a)); a.erase(unique(all(a)), a.end())
#define pb push_back
typedef long long int ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;
typedef map<ll, ll> MAP;
const ll inf = 2222222222222222222LL;
const ll mod = 1000000007LL;

ll n = 0, m = 0, ans = 0, tmp = 0, ma = -inf, mi = inf;
string s;
bool ok;
ll dx[9] = { 0,1,-1,0,0,1,1,-1,-1 }, dy[9] = { 0,0,0,1,-1,1,-1,1,-1 };
#define endl '\n'
#pragma endregion
#define MAX 222222

ll dp[MAX];

vll fac, inv, invfac;
void comb_set(ll x = MAX) {
  fac.resize(x + 1, 0);
  invfac.resize(x + 1, 0);
  inv.resize(x + 1, 0);
  fac[0] = inv[1] = invfac[0] = 1;
  FOR(i, 1, x + 1) (fac[i] = fac[i - 1] * i) %= mod;
  FOR(i, 2, x + 1) (inv[i] = (mod - mod / i) * inv[mod%i]) %= mod;
  FOR(i, 1, x + 1) (invfac[i] = invfac[i - 1] * inv[i]) %= mod;
}
ll comb(ll x, ll y) {
  if (x < y) return 0;
  return fac[x] * invfac[x - y] % mod * invfac[y] % mod;
}

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

  cin >> n >> m;
  vll v(n);
  rep(i, n) cin >> v[i];
  sort(rall(v));
  comb_set(MAX);
  dp[m] = fac[n];
  rep(i, n) {
    FOR(j, v[i], m + 1) {
      (dp[j%v[i]] += inv[n - i] * dp[j]) %= mod;
      (dp[j] = (((n - i - 1) * inv[n - i]) % mod) * dp[j]) %= mod;
    }
  }
  rep(i, m + 1) (ans += i * dp[i]) %= mod;
  cout << ans << endl;

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
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;
}

int main(){
  ll n;cin >> n;
  vvl g(n,vl(n));rep(i,n)rep(j,n)cin >> g[i][j];
  vl sum(1<<n);
  rep(bit,1<<n){
    ll ret=0;
    rep(i,n)repl(j,i+1,n)if((bit&(1<<i))&&(bit&(1<<j)))ret+=g[i][j];
    sum[bit]=ret;
  }
  //rep(i,1<<n)cout << sum[i] <<" ";cout << endl;
  rep(bit,1<<n){
    vl z;rep(i,n)if(bit&(1<<i))z.pb(i);
    rep(cit,1<<z.size()){
      ll d=0,e=0;
      rep(j,z.size()){
        if(cit&(1<<j))d+=1<<z[j];
        else e+=1<<z[j];
      }
      if(d>e)break;
      //cout << bit <<" " << d << " " << e <<endl;
      chmax(sum[bit],sum[d]+sum[e]);
    }
  }
  //rep(i,1<<n)cout << sum[i] <<" ";cout << endl;
  cout << sum[(1<<n)-1] <<endl;
}

#include <bits/stdc++.h>
#define WHOLE(v) (v).begin(), (v).end()
#define REV_WHOLE(v) (v).rbegin(), (v).rend()
using i64 = int64_t;
using namespace std;
template<class F>auto recursive(F f){return[f](auto...a){return f(f,a...);};}
template<class T>using rev_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<class T>using vector2d=vector<vector<T>>;
struct fixprec{int p;fixprec(int p):p(p){}};
ostream&operator<<(ostream&o,fixprec f){return o<<fixed<<setprecision(f.p);}

int main() {
    int N, M, lK;
    i64 K;
    cin >> N;
    vector<i64> x(N), dx(N - 1), y(N);
    for(i64 &x_ : x) cin >> x_;
    for(int i = 0; i < N - 1; i++) {
        dx[i] = x[i + 1] - x[i];
    }
    cin >> M >> K;
    vector<int> perm_base(N - 1), perm_ans(N - 1), perm_tmp(N - 1);
    for (int i = 0; i < N - 1; i++) {
        perm_base[i] = perm_ans[i] = i;
    }
    for(int i = 0; i < M; i++) {
        int a;
        cin >> a;
        a--;
        swap(perm_base[a - 1], perm_base[a]);
    }
    // 繰り返し二乗法
    for(int b = 0; (1ll << b) <= K; b++) {
        if (K & (1ll << b)) {
            for (int i = 0; i < N - 1; i++) {
                perm_ans[i] = perm_base[perm_ans[i]];
            }
        }
        for (int i = 0; i < N - 1; i++) perm_tmp[i] = i;
        for (int i = 0; i < N - 1; i++) {
            perm_tmp[i] = perm_base[perm_base[i]];
        }
        swap(perm_tmp, perm_base);
    }
    for(int i = 0; i < N; i++) {
        y[i] = i ? dx[perm_ans[i - 1]] + y[i - 1] : x[0];
        cout << y[i] << endl;
    }
    return 0;
}


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

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

const int N = 1001, M = 2001;
int n, m, a, b;
vector<int> edge[N];
vector<int> path;
unordered_map<int, int> mp;

bool check(int from, int to) {
  int s = mp[from];
  int e = mp[to];
  // index [s, e] in path
  for (int i = s; i <= e; ++i) {
    int cur = path[i];
    int next = (i == e ? path[s] : path[i + 1]);
    for (int v : edge[cur]) {
      if (v != next) {
        if (mp.count(v) && mp[v] >= s) {
          return false;
        }
      }
    }
  }
  return true;
}

void dfs(int u) {
  for (int v : edge[u]) {
    if (mp.count(v)) {
      // find cycle
      if (check(v, u)) {
        int s = mp[v];
        int e = mp[u];
        cout << e - s + 1 << endl;
        for (int i = s; i <= e; ++i) {
          cout << path[i] << endl;
        }
        exit(0);
      }
    } else {
      path.push_back(v);
      mp[v] = path.size() - 1;
      dfs(v);
      mp.erase(v);
      path.pop_back();
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> a >> b;
    edge[a].push_back(b);
  }
  for (int i = 1; i <= n; ++i) {
    path.push_back(i);
    mp[i] = 0;
    dfs(i);
    mp.erase(i);
    path.pop_back();
  }
  cout << -1 << endl;
  return 0;
}

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

int fact(int k) {
	int w = 1;
	for (int i = 2; i <= k; i++)
		w *= i;
	return w;
}

int main() {
	double sum = 0;
	std::vector<int> k;
	int n, x, y;
	std::cin >> n;
	std::vector<std::pair<int, int>> v(n);
	for (int i = 0; i < n; i++) {
		std::cin >> x >> y;
		v[i].first = x, v[i].second = y;
		k.push_back(i);
	}
	for (int i = 0; i < n - 1; i++) {
		sum += std::sqrt((v[k[i]].first - v[k[i + 1]].first) * (v[k[i]].first - v[k[i + 1]].first) +
			(v[k[i]].second - v[k[i + 1]].second) * (v[k[i]].second - v[k[i + 1]].second));
	}
	while (std::next_permutation(k.begin(), k.end())) {
		for (int i = 0; i < n - 1; i++) {
			sum += std::sqrt((v[k[i]].first - v[k[i + 1]].first) * (v[k[i]].first - v[k[i + 1]].first) +
				(v[k[i]].second - v[k[i + 1]].second) * (v[k[i]].second - v[k[i + 1]].second));
		}
	}
	printf("%.10f", sum / fact(n));
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define REP(i,x,n) for(int i = (x); i < (n); ++i)
#define ll long long
#define P pair<ll,ll>
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()

const ll mod = 1e9+7;
const ll INF = 1e18;
const double pi = acos(-1);

int main(void)
{
    ll l, r, ans=INF;
    cin >> l >> r;
    r = min(r, l+4038);
    for(ll i=l;i<=r;i++)for(ll j=i+1;j<=r;j++){
        ll tmp = ((i%2019)*(j%2019))%2019;
        ans = min(ans, tmp);
        ans %= 2019;
    }
    cout << ans << endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
long  long  l,r;

int main()
{
	scanf("%lld%lld",&l,&r);
	long long ans=2019;
	for(long long i=l;i<=r;++i)
	{
		for(long long j=i+1;j<=r;++j)
		{
			ans=min(ans,(i*j)%2019);
			if(ans==0)
			{
				cout<<ans<<endl;
				return 0;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

void dfs(int u, int prev, int *cnt, int *visited, int *prenum, int *lowest, int *parent, vector< vector<int> > &v){
  visited[u] = true;
  prenum[u] = lowest[u] = *cnt;
  REP(i, v[u].size()){
    int next = v[u][i];
    if(!visited[next]){
      parent[next] = u;
      ++(*cnt);
      dfs(next, u, cnt, visited, prenum, lowest, parent, v);
      lowest[u] = min(lowest[u], lowest[next]);
    } else if(next != prev)
      lowest[u] = min(lowest[u], prenum[next]);
  }
}

vector<int> articulation_points(int V, int E, vector< vector<int> > &v){
  int visited[V], prenum[V], lowest[V], parent[V];
  memset(visited, 0, sizeof(visited));
  parent[0] = -1;
  int tmp = 1;
  dfs(0, -1, &tmp, visited, prenum, lowest, parent, v);
  //cout <<"prenum" <<endl;
  //REP(i, V) cout <<i <<" : " <<prenum[i] <<endl;
  //cout <<"parent" <<endl;
  //REP(i, V) cout <<i <<" : " <<parent[i] <<endl;
  //cout <<"lowest" <<endl;
  //REP(i, V) cout <<i <<" : " <<lowest[i] <<endl;
  vector<int> ret;
  int cnt = 0, used[V];
  memset(used, 0, sizeof(used));
  FOR(i, 1, V){
    if(parent[i] == 0) ++cnt;
    if(parent[i] > 0 && !used[parent[i]] && prenum[parent[i]] <= lowest[i]){
      used[parent[i]] = true;
      ret.push_back(parent[i]);
    }
  }
  if(cnt >= 2) ret.push_back(0);
  return ret;
}

int main() {
  int V, E; cin >>V >>E;
  vector< vector<int> > es(V);
  REP(i, E){
    int f, t; cin >>f >>t;
    es[f].push_back(t);
    es[t].push_back(f);
  }
  vector<int> ans = articulation_points(V, E, es);
  SORT(ans);
  REP(i, ans.size()) cout <<ans[i] <<endl;
  return 0;
}


#include<cstdio>
#define RI int
#define CI const int&
using namespace std;
const int N=6005;
int n,mod,f[N][N<<1],ans;
inline void inc(int& x,CI y)
{
	if ((x+=y)>=mod) x-=mod;
}
int main()
{
	RI i,j,k,t; scanf("%d%d",&n,&mod); n*=3; f[0][n]=1;
	for (i=0;i<n;++i) for (j=-i;j<=i;++j) if (f[i][n+j])
	inc(f[i+1][n+j-1],f[i][n+j]),
	inc(f[i+2][n+j+1],1LL*f[i][n+j]*(i+1)%mod),
	inc(f[i+3][n+j],1LL*f[i][n+j]*(i+2)%mod*(i+1)%mod);
	for (i=-n;i<=0;++i) inc(ans,f[n][n+i]); return printf("%d",ans),0;
}

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

using namespace std;

int main(){
    int N, MOD;
    while(cin >> N >> MOD){
        const int maxSize = 3*N+1;
        vector<long long> inv(maxSize);
        vector<long long> fact(maxSize);
        for(int i=0;i<2;i++) inv[i] = fact[i] = 1;
        for(int i=2;i<maxSize;i++){
            inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
            fact[i] = fact[i-1] * i % MOD;
        }
        vector<vector<long long>> dp(maxSize, vector<long long>(4*N+1, 0));
        dp[0][N] = 1;
        for(int i=0;i<3*N;i++){
            for(int j=0;j<=4*N;j++){
                int dj[] = {1, -1, 0};
                for(int k=0;k<3;k++){
                    if(j+dj[k] < 0 || 4*N < j+dj[k]) continue;
                    if(i+k+1 > 3*N) continue;
                    dp[i+k+1][j+dj[k]] += dp[i][j] * inv[i+k+1];
                    dp[i+k+1][j+dj[k]] %= MOD;
                }
            }
        }
        long long res = 0;
        for(int i=N;i<=4*N;i+=3) res = (res + dp[3*N][i]) % MOD;
        cout << (fact[3*N] * res) % MOD << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define rep(i, n) for(ll i=0; i<(ll)(n); i++)
#define repd(i, n) for(ll i=n-1; 0<=i; i--)
#define FOR(i, a, b) for(ll i=a; i<(ll)(b); i++)
#define FORD(i, a, b) for(ll i=a-1; (ll)(b)<=i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double dp[310][310][310];
ll n;

double calc(int x, int y, int z){
    double ret=0.0;
    if(0<=dp[x][y][z]) return dp[x][y][z];
    if(!x && !y && !z) return 0.0;
    if(0<x) ret += calc(x-1, y, z)*x;
    if(0<y) ret += calc(x+1, y-1, z)*y;
    if(0<z) ret += calc(x, y+1, z-1)*z;
    ret += n;
    ret *= 1.0/(x+y+z);
    return dp[x][y][z] = ret;
}

int main(){
    cin >> n;
    vector<ll> a(n);
    ll c1=0, c2=0, c3=0;
    rep(i, n){
        ll x;   cin >> x;
        if(x==1) c1++;
        else if(x==2) c2++;
        else c3++;
    }
    memset(dp, -1, sizeof(dp));
    cout << fixed << setprecision(20) << calc(c1, c2, c3) << endl;
}

#include "bits/stdc++.h"
#define ll long long
#define mp(a, b) make_pair(a, b)
using namespace std;
typedef pair<ll, ll> pairs;
typedef pair<ll, pairs> tpl;
ll c[4];
double dp[301][301][301];
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll n, a;
    cin >> n;
    for (ll i = 0; i < n; i++)
        cin >> a, c[a]++;
    dp[0][0][0] = 0;
    for (ll i = 0; i <= n; i++)
    {
        for (ll j = 0; j <= n; j++)
        {
            for (ll k = 0; k <= n; k++)
            {
                double z = n - (i + j + k), val = 1;
                if (z == n)
                    continue;
                if (i + j + k > n)
                    continue;
                if (i > 0)
                    val += (1.0 * i / n) * dp[i - 1][j + 1][k];
                if (j > 0)
                    val += (1.0 * j / n) * dp[i][j - 1][k + 1];
                if (k > 0)
                    val += (1.0 * k / n) * dp[i][j][k - 1];
                if (z > 0)
                    val /= (1.0 - 1.0 * z / n);
                dp[i][j][k] = val;
            }
        }
    }

    cout << setprecision(9) << fixed << dp[c[3]][c[2]][c[1]];
}


#include <bits/stdc++.h>
#include <iomanip>
#include <math.h>
//#include <unistd.h>
//#include <stdio.h>
#define rep(i,x) for(ll i = 0;i<x;i++)
#define all(a) (a).begin(),(a).end()
using ll = long long;
using ld = long double;
using namespace std;
using dou = double;
const ll INF = 1LL << 60;
const ll mod = 1000000007;
typedef pair<ll,ll> P;
using graph = vector<vector<ll>>;
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;}
ll gcd(ll a,ll b){
    if(a < b)swap(a,b);
    if(a % b == 0)return b;
    else return gcd(b,a%b);
}
//素数判定O(sqrt(N))
bool isp(ll n){
    bool res = true;
    if(n == 1)return false;
    else{
        for(ll i = 2;i * i <= n;i++){
            if(n % i == 0){
                res = false;
                break;
            }
        }
        return res;
    }
}

ll fac[510000], finv[510000], inv[510000];

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

// 二項係数計算
ll COM(ll n, ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
//nのm乗をMODで割ったあまりO(logN)
ll modpow(ll n,ll m,ll MOD){
    ll res = 1;
    while(m){
        if(m & 1)res = (res * n) % MOD;
        m >>= 1;
        n *= n;
        n %= MOD;
    }
    return res;
}

//printf("%.10f\n", N);
/*  vector<ll> vec = {1,2,5,6,8,9,10};
    cout << binary_search(all(vec),5) << endl; -> true*/
/*
    vector<ll> vec = {1,2,5,6,8,9,10};
    auto n = upper_bound(all(vec),4);
    cout << *n << endl; -> 5*/
/*
    cout << fixed << setprecision(15);
*/
int main(){
    string S;
    cin >> S;
    if(S.size() == 2){
        cout << S << endl;return 0;
    }
    else{
        reverse(all(S));
        cout << S << endl;
    }
}

const int LG = 21;
const int FN = 400005;
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
const long long INFLL = 1e18;

#include <bits/stdc++.h>

using namespace std;

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

#define forn(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define fi first
#define se second
#define re return
#define pb push_back
#define uniq(x) sort(all(x)); (x).resize(unique(all(x)) - (x).begin())

#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " " << #x << " " << x << endl
#define ln cerr << __LINE__ << endl
#else
#define dbg(x) void(0)
#define ln void(0)
#endif // LOCAL

int cx[4] = {-1, 0, 1, 0};
int cy[4] = {0, -1, 0, 1};
string Ans[2] = {"No", "Yes"};
string ANS[2] = {"NO", "YES"};

ll inq(ll x, ll y)
{
    if (!y) re 1 % MOD;
    ll l = inq(x, y / 2);
    if (y % 2) re l * l % MOD * x % MOD;
    re l * l % MOD;
}

ll rev(ll x)
{
    return inq(x, MOD - 2);
}

bool __precomputed_combinatorics = 0;
vector<ll> __fact, __ufact, __rev;

void __precompute_combinatorics()
{
    __precomputed_combinatorics = 1;
    __fact.resize(FN);
    __ufact.resize(FN);
    __rev.resize(FN);
    __rev[1] = 1;
    for (int i = 2; i < FN; i++) __rev[i] = MOD - __rev[MOD % i] * (MOD / i) % MOD;
    __fact[0] = 1, __ufact[0] = 1;
    for (int i = 1; i < FN; i++) __fact[i] = __fact[i - 1] * i % MOD, __ufact[i] = __ufact[i - 1] * __rev[i] % MOD;
}

ll fact(int x)
{
    if (!__precomputed_combinatorics) __precompute_combinatorics();
    return __fact[x];
}

ll cnk(int n, int k)
{
    if (k < 0 || k > n) return 0;
    if (!__precomputed_combinatorics) __precompute_combinatorics();
    return __fact[n] * __ufact[n - k] % MOD * __ufact[k] % MOD;
}

int Root(int x, vector<int> &root)
{
    if (x == root[x]) return x;
    return root[x] = Root(root[x], root);
}

void Merge(int v, int u, vector<int> &root, vector<int> &sz)
{
    v = Root(v, root), u = Root(u, root);
    if (v == u) return;
    if (sz[v] < sz[u])
    {
        sz[u] += sz[v];
        root[v] = u;
    }
    else
    {
        sz[v] += sz[u];
        root[u] = v;
    }
}

int ok(int x, int n)
{
    return 0 <= x && x < n;
}

void bfs(int v, vi &dist, vector<vi> &graph)
{
    fill(all(dist), -1);
    dist[v] = 0;
    vi q = {v};
    for (int i = 0; i < q.size(); i++)
    {
        for (auto u : graph[q[i]])
        {
            if (dist[u] == -1)
            {
                dist[u] = dist[q[i]] + 1;
                q.push_back(u);
            }
        }
    }
}

int t;

void dfs(int v, int p, G &graph, vi &in, vi &up)
{
    up[v] = in[v] = t;
    t++;
    for (auto u : graph[v])
    {
        if (u == p) continue;
        if (in[u] == 0)
        {
            dfs(u, v, graph, in, up);
        }
        up[v] = min(up[v], up[u]);
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    string s;
    cin >> s;
    if (s.size() == 3) reverse(all(s));
    cout << s;
}

/* Note:
Check constants at the beginning of the code.
    N is set to 4e5 but be careful in problems with large constant factor.
    Setting N in every problem is more effective.
Check corner cases.
    N = 1
No def int long long for now.
Add something here.
*/


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <queue>
#include <vector>

using namespace std;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

int n,D1,D2,id[650][650],col[400000][3];

vector <pair <int,int>> d[3];

inline void dfs(int u,int c)
{
	int x=(u-1)/(2*n)+1,y=(u-1)%(2*n)+1;
	//cout << x << " " << y << endl;
	for (int i=0;i<d[c].size();i++)
	{
		pair <int,int> rhs=d[c][i];
		int nx=x+rhs.first,ny=y+rhs.second;
		if (nx>=1 && nx<=2*n && ny>=1 && ny<=2*n)
		{
			int rid=id[nx][ny];
			if (!col[rid][c])
			{
				col[rid][c]=col[u][c]==1?2:1;
				dfs(rid,c);
			}
		}
	}
}

int main()
{
	n=read(),D1=read(),D2=read();
	for (int i=0;i<=D1;i++)
	{
		int j=sqrt(D1-i*i);
		if (i*i+j*j==D1)
		{
			d[1].push_back({i,j});
			d[1].push_back({-i,j});
			d[1].push_back({i,-j});
			d[1].push_back({-i,-j});
			//if (i!=0)
			//	break;
		}
	}
	for (int i=0;i<=D2;i++)
	{
		int j=sqrt(D2-i*i);
		if (i*i+j*j==D2)
		{
			d[2].push_back({i,j});
			d[2].push_back({-i,j});
			d[2].push_back({i,-j});
			d[2].push_back({-i,-j});
			//if (i!=0)
			//	break;
		}
	}
	for (int i=1;i<=n*2;i++)
	{
		for (int j=1;j<=n*2;j++)
			id[i][j]=(i-1)*2*n+j;
	}
	for (int i=1;i<=n*2;i++)
	{
		for (int j=1;j<=n*2;j++)
		{
			int rhs=id[i][j];
			if (!col[rhs][1])
			{
				col[rhs][1]=1;
				dfs(rhs,1);
			}
			if (!col[rhs][2])
			{
				col[rhs][2]=1;
				dfs(rhs,2);
			}
		}
	}
	int cnt=0;
	for (int i=1;i<=n*2;i++)
	{
		for (int j=1;j<=n*2;j++)
		{
			int rid=id[i][j];
			if (col[rid][1]==1 && col[rid][2]==1)
			{
				cout << i-1 << " " << j-1 << endl;
				cnt++;
			}
			if (cnt==n*n)
				return 0;
		}
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> pi;
#define f first
#define s second
#define FAST ios_base::sync_with_stdio(0); cin.tie(0);
#define all(x) x.begin(),x.end()
const int maxn = 6010;
const int mod = 998244353;

int fac[maxn];
map <int,int> mp;

int expo(int x, int exp) {
	int res = 1, base = x;
	while (exp) {
		if (exp & 1) res = (res * base) % mod;
		base = (base*base) % mod;
		exp >>= 1;
	}
	return res;
}

int inv(int x) {
	if (mp.find(x) != mp.end()) return mp[x];
	else return mp[x] = expo(x,mod-2);
}

int choose(int N, int k) {
	return fac[N] * inv(fac[k]*fac[N - k] % mod) % mod;
}

int K,n;

int32_t main() {
	FAST
	cin >> K >> n;
	fac[0] = 1;
	for (int i =1; i<maxn;i++) fac[i] = fac[i-1] * i % mod;

	for (int i = 2;i <= 2 * K; i++) {
		int ans = 0;
		int pairs = i/2 - max(1ll, i - K) + 1;

		for (int x = 0; x <= pairs; x++) {
			if (n < x * 2) break;
			int v = choose(n - x*2 + K -1, n - x * 2) * choose(pairs,x) % mod;
			if (x % 2 == 0) ans = (ans + v) % mod;
			else ans = (ans - v + mod) % mod;
			//cout << i << " " << x << " " << v << " " << ans << "\n";
		}
		cout << ans << "\n";
	}
}


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

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

    set<int> excluded;
    int ans = 1e6;
    for (int k = 0; k < m; k++) {
        vector<int> counter(m + 1, 0);
        int toBeExcluded = 0;
        for (int i = 0; i < n; i++) {
            int j = 0;
            while (j < m && excluded.count(a[i][j])) {
                j++;
            }
            counter[a[i][j]]++;
            if (counter[toBeExcluded] < counter[a[i][j]]) {
                toBeExcluded = a[i][j];
            }
        }
        ans = min(ans, counter[toBeExcluded]);
        excluded.insert(toBeExcluded);
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f3f3f3f3f
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fi first
#define se second
#define N 305

using namespace std;
typedef pair<int,int> Pair;
const int mod = 1e9 + 7;
inline int add(int x, int y) { return (x += y) - (x >= mod ? mod : 0); }
inline void inc(int &x, int y) { (x += y) -= (x >= mod ? mod : 0); }
inline int mul(int x, int y) { return 1ll * x * y % mod; }

template <class T> inline T	input() {
	int x; char ch; while (!isdigit(ch = getchar()));
	for (x = ch ^ 48; isdigit(ch = getchar()); x = x * 10 + (ch ^ 48));
	return x;
}

template <class T> inline void chkmin(T &x, T &y) { x = x < y ? x : y; }
template <class T> inline void chkmax(T &x, T &y) { x = x > y ? x : y; }

struct {
	inline operator int () { return input<int>(); }
	inline operator long long () { return input<long long>(); }
	template <class T> inline void operator () (T &x) { x = *this; }
	template<class T, class ...A> inline void operator () (T &x, A &...a)
	{ x = *this; this -> operator ()(a...); }
} read;

int n, m, ans = inf;
int a[N][N];
int cnt[N];
bool vis[N];

void calc() {
	memset(cnt, 0, sizeof cnt);
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)
			if (vis[a[i][j]]) {
				++cnt[a[i][j]];
				break;
			}
}

int main() {
	read(n, m);
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)
			read(a[i][j]);

	for (int i = 1; i <= m; ++i) vis[i] = true;
	calc();

	for (int i = 1; i <= m; ++i) {
		int pos = max_element(cnt + 1, cnt + m + 1) - cnt;
		ans = min(ans, cnt[pos]);
		vis[pos] = false;
		calc();
	}

	printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <iomanip>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define repr(e,x) for(auto& e:x)
using namespace std;
typedef long long ll;
typedef pair<ll,int> P;

ll N;
ll A[100000], B[100000];
ll sum[100000];

int main(){
    cin>>N;
    rep(i,N){
        cin>>A[i]>>B[i];
        sum[i]=A[i]+B[i];
    }

    ll ans=0;
    rep(i,N) ans-=B[i];

    sort(sum,sum+N,greater<>());

    for(int i=0;i<N;i+=2) ans+=sum[i];

    cout<<ans<<endl;
}

#include <bit>
#include <bitset>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <queue>
#include <stack>

typedef long double ld;
typedef long long ll;

using namespace std;

struct dish {
	ll x, y;

	dish(ll tx = 0, ll ty = 0) {
		x = tx;
		y = ty;
	}

	void read() {
		cin >> x >> y;
	}

	bool operator<(const dish & d) const {
		return (x + y > d.x + d.y);
	}
};

#define MAXN (1 << 17)

dish a[MAXN];
int n;

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		a[i].read();
	}
	sort(a, a + n);
	ll ret = 0;
	for (int i = 0; i < n; i++) {
		if (i & 1) {
			ret -= a[i].y;
		} else {
			ret += a[i].x;
		}
	}
	cout << ret << endl;

	return 0;
}


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

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

typedef long long ll;
typedef pair<int, int> pii;
#define mp make_pair

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

const int LOG = 20;
const int N = 1 << 20;
ll w[N];
int rev[N];

void initFFT() {
	for (ll x = 2;; x++) {
		ll y = x;
		for (int i = 1; i < LOG; i++)
			y = mult(y, y);
		if (y == MOD - 1) {
			W = x;
			break;
		}
	}
	w[0] = 1;
	for (int i = 1; i < N; i++)
		w[i] = mult(w[i - 1], W);
	rev[0] = 0;
	for (int mask = 1; mask < N; mask++) {
		int k = 0;
		while(((mask >> k) & 1) == 0) k++;
		rev[mask] = rev[mask ^ (1 << k)] ^ (1 << (LOG - 1 - k));
	}
}

ll F[2][N];
void FFT(ll *A) {
	for (int i = 0; i < N; i++)
		F[0][rev[i]] = A[i];
	int t = 0, nt = 1;
	for (int lvl = 0; lvl < LOG; lvl++) {
		int len = 1 << lvl;
		for (int st = 0; st < N; st += (len << 1))
			for (int i = 0; i < len; i++) {
				ll ad = mult(F[t][st + len + i], w[i << (LOG - 1 - lvl)]);
				F[nt][st + i] = add(F[t][st + i], ad);
				F[nt][st + len + i] = sub(F[t][st + i], ad);
			}
		swap(t, nt);
	}
	for (int i = 0; i < N; i++)
		A[i] = F[t][i];
}

ll f[N], rf[N];
ll a[N];

ll getC(int n, int k) {
	if (k < 0 || k > n) return 0;
	return mult(f[n], mult(rf[k], rf[n - k]));
}

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

	initFFT();

	f[0] = 1;
	for (int i = 1; i < N; i++)
		f[i] = mult(f[i - 1], i);
	rf[N - 1] = Rev(f[N - 1]);
	for (int i = N - 1; i > 0; i--)
		rf[i - 1] = mult(rf[i], i);

	for (int k = 0; k < N / 2; k++) {
		a[k] = mult(f[2 * k], mult(rf[k], rf[k]));
	}
	FFT(a);
	for (int i = 0; i < N; i++)
		a[i] = mult(a[i], a[i]);
	FFT(a);
	reverse(a + 1, a + N);
	ll rN = Rev(N);
	for (int i = 0; i < N; i++) {
		a[i] = mult(a[i], rN);
		a[i] = mult(a[i], mult(rf[2 * i], mult(f[i], f[i])));
		a[i] = sub(a[i], 1);
		a[i] = mult(a[i], (MOD + 1) / 2);
		a[i] += i;
	}

	int n, m;
	scanf("%d%d", &n, &m);
	if (n < m) swap(n, m);
	if (n == m) {
		printf("%lld\n", a[n]);
		return 0;
	}

	ll ans = 0;

	for (int k = 0; k <= m; k++) {
		int L = n - m + k - 1;
		ll p = getC(L + k, k);
		if (k > 0)
			p = sub(p, getC(L + k, k - 1));
		p = mult(p, getC(2 * (m - k), m - k));
		ans = add(ans, mult(p, add(L + 1, a[m - k])));
	}
	ans = mult(ans, mult(rf[n + m], mult(f[n], f[m])));
	printf("%lld\n", ans);

	return 0;
}


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

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

int main() {
    fast_io;
    long long x;
    cin>>x;

    long long thousand=(x/500)*1000;

    long long rest=(x%500)/5*5;;

    long long ans=thousand+rest;
    cout<<ans<<endl;
}

#include<bits/stdc++.h>
// Begin Header {{{
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i,n) for(ll i=0; i<n; i++)
#define loop(i, j, n) for(ll i=j; i<n; i++)
#define all(x) (x).begin(), (x).end()
constexpr int INF  = 0x3f3f3f40;
const long long mod=2e9+7;
const long double PI = acos(0);
// }}} End Header
int main() {
	ll x, ans=0;
	cin >> x;
	ans+=1000*(x/500);
	x%=500;
	ans+=5*(x/5);
	cout << ans << endl;
	return 0;
}


#include "bits/stdc++.h"

#define MOD 1000000007
#define rep(i, n) for(ll i=0; i < (n); i++)
#define rrep(i, n) for(ll i=(n)-1; i >=0; i--)
#define ALL(v) v.begin(),v.end()
#define rALL(v) v.rbegin(),v.rend()
#define FOR(i, j, k) for(ll i=j;i<k;i++)
#define debug_print(var) cerr << #var << "=" << var <<endl;
#define DUMP(i, v)for(ll i=0;i<v.size();i++)cerr<<v[i]<<" "
#define fi first
#define se second

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge{ll x, c;};

ll mod(ll a, ll mod){
  ll res = a%mod;
  if(res<0)res=res + mod;
  return res;
}

ll modpow(ll a, ll n, ll mod){
  ll res=1;
  while(n>0){
    if(n&1) res=res*a%mod;
    a=a*a%mod;
    n>>=1;
  }
  return res;
}

ll modinv(ll a, ll mod){
  ll b=mod, u=1, v=0;
  while(b){
    ll t=a/b;
    a-=t*b; swap(a, b);
    u-=t*v; swap(u, v);
  }
  u%=mod;
  if(u<0)u+=mod;
  return u;
}

ll gcd(ll a, ll b){
  ll r = a%b;
  if(r==0) return b;
  else return gcd(b, a%b);
}

bool is_prime(ll n){
  ll i = 2;
  if(n==1)return false;
  if(n==2)return true;
  bool res = true;
  while(i*i <n){
    if(n%i==0){
      res = false;
    }
    i = i+1;
  }

  //if(i==1)res = false;
  if(n%i==0)res=false;
  return res;
}

struct UnionFind{
  ll N;
  llvec p;
  llvec cnt;
  UnionFind(ll n){
    N = n;
    p=llvec(N);
    cnt=llvec(N, 0);
    rep(i, N){
      p[i] = i;
      cnt[i] = 1;
    }
  }

  void con(ll a, ll b){
    P at = root(a);
    P bt = root(b);
    if(at.second!=bt.second){
      if(at.first>bt.first){
        swap(at, bt);
        swap(a, b);
      }
      p[at.second] = bt.second;
      cnt[bt.second]+=cnt[at.second];
      p[a]=bt.second;
    }
  }

  P root(ll a){
    ll atmp = a;
    ll c=0;
    while(atmp!=p[atmp]){
      atmp = p[atmp];
      c++;
    }
    p[a] = atmp;
    return {c, atmp};
  }

  bool is_con(ll a, ll b){
    P at=root(a);
    P bt=root(b);
    return at.second == bt.second;
  }

};

struct dijkstra{
  ll N;
  llvec d;
  vector<vector<edge>> e;
  dijkstra(ll n){
    N = n;
    //d = llvec(N, 1e18);
    e = vector<vector<edge>>(N);
  }

  void add_edge(ll from, ll to, ll cost){
    e[from].push_back({to, cost});
  }

  void run(ll start){
    priority_queue<P, vector<P>, greater<P>> que;
    que.push({0, start});
    d = llvec(N, 1e18);
    d[start]=0;
    while(!que.empty()){
      P q = que.top();que.pop();
      ll dc = q.first;
      ll x = q.second;
      if(dc>d[x]){
        continue;
      }else{
        for(auto ip: e[x]){
          if(d[ip.x]<=d[x]+ip.c){
            continue;
          }else{
            d[ip.x]= d[x]+ip.c;
            que.push({d[ip.x], ip.x});
          }
        }
      }
    }
  }
};

/**************************************
** A main function starts from here  **
***************************************/
int main(){
  ll a, b;
  cin >> a >> b;
  ll k;
  cin >> k;
  for(ll i=0;i<k;i++){
    if(a%2==1)a-=1;
    b += a/2;
    a/=2;
    i++;
    if(i==k)break;
    if(b%2==1)b-=1;
    a += b/2;
    b/=2;
  }
  cout << a << " " << b << endl;
  return 0;
}

#include <bits/stdc++.h>

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

using namespace std;

using ll = long long;
using P = pair<int, int>;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> x(n);
  rep(i, n) cin >> x.at(i);
  vector<int> minus;
  vector<int> plus;
  rep(i, n) {
    int xx = x.at(i);
    if (xx >= 0) {
      plus.push_back(xx);
    } else {
      minus.push_back(-xx);
    }
  }
  reverse(minus.begin(), minus.end());
  ll res = 1L << 30;
  // マイナスから折り返す
  rep(i, min((int) minus.size(), k)) {
    ll tmp = 0;
    tmp += 2 * minus.at(i);
    int plusCnt = k - 1 - i;
    if (plusCnt > (int) plus.size()) {
      continue;
    }
    if (plusCnt > 0) {
      tmp += plus.at(plusCnt - 1);
    }
    res = min(res, tmp);
  }
  // プラスから折り返す
  rep(i, min((int) plus.size(), k)) {
    ll tmp = 0;
    tmp += 2 * plus.at(i);
    int minusCnt = k - 1 - i;
    if (minusCnt > (int) minus.size()) {
      continue;
    }
    if (minusCnt > 0) {
      tmp += minus.at(minusCnt - 1);
    }
    res = min(res, tmp);
  }
  // 全部どちらかの場合
  if ((int) minus.size() >= k) {
    res = min(res, (ll) minus.at(k - 1));
  }
  if ((int) plus.size() >= k) {
    res = min(res, (ll) plus.at(k - 1));
  }
  cout << res << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
  int m;
  cin >> m;
  vector<int> nodes;
  vector<pair<int, int>> child;
  int n = 0;
  for (int i = 0; i < m; i++){
    string q;
    cin >> q;
    if (q == "insert"){
      int k;
      cin >> k;
      if (n != 0){
        int v = 0;
        while (1){
          if (k < nodes[v]){
            if (child[v].first != -1){
              v = child[v].first;
            } else {
              child[v].first = nodes.size();
              break;
            }
          } else {
            if (child[v].second != -1){
              v = child[v].second;
            } else {
              child[v].second = nodes.size();
              break;
            }
          }
        }
      }
      nodes.push_back(k);
      child.push_back(make_pair(-1, -1));
      n++;
    }
    if (q == "find"){
      int k;
      cin >> k;
      if (n == 0){
        cout << "no" << endl;
      } else {
        int v = 0;
        while (1){
          if (k < nodes[v]){
            if (child[v].first == -1){
              cout << "no" << endl;
              break;
            } else {
              v = child[v].first;
            }
          } else if (k > nodes[v]){
            if (child[v].second == -1){
              cout << "no" << endl;
              break;
            } else {
              v = child[v].second;
            }
          } else {
            cout << "yes" << endl;
            break;
          }
        }
      }
    }
    if (q == "delete"){
      int k;
      cin >> k;
      int v = 0;
      int u;
      while (1){
        if (k < nodes[v]){
          u = v;
          v = child[v].first;
        } else if (k > nodes[v]){
          u = v;
          v = child[v].second;
        } else {
          break;
        }
      }
      if (child[v].first != -1 && child[v].second != -1){
        u = v;
        int w = child[v].second;
        while (child[w].first != -1 || child[w].second != -1){
          u = w;
          if (child[w].first != -1){
            w = child[w].first;
          } else {
            w = child[w].second;
          }
        }
        swap(nodes[v], nodes[w]);
        v = w;
      }
      if (child[v].first != -1 && child[v].second == -1){
        int w = child[v].first;
        nodes[v] = nodes[w];
        child[v].first = child[w].first;
        child[v].second = child[w].second;
      } else if (child[v].second != -1 && child[v].first == -1){
        int w = child[v].second;
        nodes[v] = nodes[w];
        child[v].first = child[w].first;
        child[v].second = child[w].second;
      } else {
        if (child[u].first == v){
          child[u].first = -1;
        } else {
          child[u].second = -1;
        }
      }
      n--;
    }
    if (q == "print"){
      //Inorder
      if (n != 0){
        stack<int> S;
        vector<int> t(nodes.size(), 0);
        S.push(0);
        while (!S.empty()){
          int v = S.top();
          S.pop();
          if (t[v] == 1){
            cout << ' ' << nodes[v];
          }
          if (t[v] == 0){
            S.push(v);
            if (child[v].second != -1){
              S.push(child[v].second);
            }
            S.push(v);
            if (child[v].first != -1){
              S.push(child[v].first);
            }
          }
          t[v]++;
        }
      }
      cout << endl;
      //Preorder
      if (n != 0){
        stack<int> S;
        vector<int> t(nodes.size(), 0);
        S.push(0);
        while (!S.empty()){
          int v = S.top();
          S.pop();
          if (t[v] == 0){
            cout << ' ' << nodes[v];
          }
          if (t[v] == 0){
            S.push(v);
            if (child[v].second != -1){
              S.push(child[v].second);
            }
            S.push(v);
            if (child[v].first != -1){
              S.push(child[v].first);
            }
          }
          t[v]++;
        }
      }
      cout << endl;
    }
  }
}

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

typedef long long ll;

struct Node{
    ll key;
    Node *parent,*left,*right;
};

struct Tree{
    Node *nil,*root;

    void init(){
        root=nil;
    }

    void add(ll key){
        Node *newnode;
        Node *p;
        newnode = (Node*)malloc(sizeof(Node));

        newnode->key = key;
        newnode->parent = newnode->right = newnode->left = nil;

        Node *pa = find_node(key);
        if(pa == nil){
            root = newnode;
        }else if(key < pa->key){
            pa->left = newnode;
            newnode->parent = pa;
        }else{
            pa->right = newnode;
            newnode->parent = pa;
        }
    }

    Node *find_node(ll key){
        Node *pa=nil;
        Node *loc=root;
        while(loc != nil){
            pa = loc;
            if(key < loc->key){
                loc = loc->left;
            }else{
                loc = loc->right;
            }
        }
        return pa;
    }

    Node *find_x(ll x){
        Node *p=root;
        while(p != nil){
            if(x == p->key){
                break;
            }else if(x < p->key){
                p = p->left;
            }else{
                p = p->right;
            }
        }
        return p;
    }

    ll find_rightmin(Node *r){
        Node *rmin = r->right;
        while(rmin->left != nil){
            rmin = rmin->left;
        }
        return rmin->key;
    }

    void del(ll k){
        Node *loc=find_x(k);
        Node *p=loc->parent;
        if(loc->left==nil && loc->right == nil){
            //leafだったら
            if(p==nil)root=nil;
            else if(p->key > loc->key)p->left=nil;
            else p->right=nil;
            free(loc);
        }else if(loc->left == nil || loc->right==nil){
            //片方だったら
            if(loc->left == nil){
                if(p == nil)root=loc->right, loc->right->parent=root;
                else if(p->key > loc->key)p->left=loc->right, loc->right->parent=p;
                else p->right=loc->right, loc->right->parent=p;
            }else{
                if(loc->parent == nil)root=loc->left, loc->left->parent=nil;
                else if(p->key > loc->key)p->left=loc->left, loc->left->parent=p;
                else p->right=loc->left, loc->left->parent=p;
            }
            free(loc);
        }else{
            //二分木の根だったら
            ll rmin=find_rightmin(loc);
            del(rmin);
            loc->key=rmin;
        }
    }

    void print_in(Node *r){
        if(r==nil)return;
        else{
            print_in(r->left);
            cout << " " << r->key;
            print_in(r->right);
        }
    }

    void print_pre(Node *r){
        if(r==nil)return;
        else{
            cout << " " << r->key;
            print_pre(r->left);
            print_pre(r->right);
        }
    }
    void print(){
        print_in(root);
        cout << endl;
        print_pre(root);
        cout << endl;
    }
};

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

    Tree tree;
    tree.init();
    int n;
    cin >> n;
    string s;
    for(int i=0;i<n;i++){
        cin >> s;
        if(s=="insert"){
            ll k;
            cin >> k;
            tree.add(k);
        }else if(s=="find"){
            ll k;
            cin >> k;
            Node *p = tree.find_x(k);
            if(p != tree.nil)cout << "yes\n";
            else cout << "no\n";
        }else if(s=="delete"){
            ll k;
            cin >> k;
            tree.del(k);
        }else {
            tree.print();
        }
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
#include <cmath>
#include <queue>

using namespace std;

int main(){

	int n;

	while(true){
		cin >> n;
		if(n == 0){
			break;
		}

		for(int i = 0; i < n; i++){
			int a, b, c;
			cin >> a >> b >> c;
			if(a == 100 || b == 100 || c == 100){
				cout << "A" << endl;
			}else if(a + b >= 180){
				cout << "A" << endl;
			}else if(a + b + c >= 240){
				cout << "A" << endl;
			}else if(a + b + c >= 210){
				cout << "B" << endl;
			}else if(a + b + c >= 150 && (a >= 80 || b >= 80)){
				cout << "B" << endl;
			}else{
				cout << "C" << endl;
			}
		}

	}

	return 0;
}

/*
 * src.cpp
 *
 *  Created on: 2016/08/04
 *      Author: joi
 */

#include <bits/stdc++.h>

using namespace std;

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

const int MAX_N=1e4;

int n;
int p[3];

char out[MAX_N+5];

int main(){
	do{
		cin>>n;
		if (n){
			REP(i,n){
				int ave=0;
				bool f=false;
				REP(j,3){
					cin>>p[j];
					ave+=p[j];
					if (p[j]==100){
						f=true;
					}
				}
				if (f){
					out[i]='A';
				}else if (p[0]+p[1]>=180){
					out[i]='A';
				}else if (ave>=80*3){
					out[i]='A';
				}else if (ave>=70*3){
					out[i]='B';
				}else if (ave>=50*3 && (p[0]>=80 || p[1]>=80)){
					out[i]='B';
				}else{
					out[i]='C';
				}
			}
			REP(i,n){
				cout<<out[i]<<endl;
			}
		}
	}while (n);
	return 0;
}


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

using namespace std;

int main () {

	int n;
	vector<int> values;

	cin >> n;
	cin.ignore();

	for (int i = 1; i <= n; i++){
		string tmp_value;

		if(i == n)
			getline(cin, tmp_value);
		else
			getline(cin, tmp_value, ' ');

		values.push_back(stoi(tmp_value));
	}

	for (int i = n; i != 0; i--){

		if (i == 1)
			cout << values[i - 1] << endl;
		else
			cout << values[i - 1] << ' ';
	}

	return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main(){
  int b,a[100];
  cin>>b;
  for(int i=0;i<b;i++){
    cin>>a[i];
  }
  for(int i=b-1;i>=0;i--){
    cout<<a[i];
    if(i!=0)cout<<" ";
  }
  cout<<endl;

    return 0;
}


/*  */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000")

#define sti				set<int>
#define stpii			set<pair<int, int> >
#define mpii			map<int,int>
#define vi				vector<int>
#define pii				pair<int,int>
#define vpii			vector<pair<int,int> >
#define rep(i, a, n) 	for (int i=a;i<n;++i)
#define per(i, a, n) 	for (int i=n-1;i>=a;--i)
#define clr				clear
#define pb 				push_back
#define mp 				make_pair
#define fir				first
#define sec				second
#define all(x) 			(x).begin(),(x).end()
#define SZ(x) 			((int)(x).size())
#define lson			l, mid, rt<<1
#define rson			mid+1, r, rt<<1|1
#define getBits(x)		__builtin_popcount(x)
#define getBitIdx(x)	__builtin_ffs(x)

typedef long long LL;
const int maxn = 45;
int s[maxn][maxn];
int dir[4][2] = {
	-1,0, 1,0, 0,-1, 0,1
};
int n, m;
int stx, sty, edx, edy;
int ansd;

inline bool judge(int x, int y) {
	return x<0 || x>=n || y<0 || y>=m;
}

void init() {
	rep(i, 0, n) {
		rep(j, 0, m) {
			if (s[i][j] == 2) {
				stx = i;
				sty = j;
				s[i][j] = 0;
			} else if (s[i][j] == 3) {
				edx = i;
				edy = j;
				s[i][j] = 0;
			}
		}
	}
}

void dfs(int dep, int x, int y) {
	if (dep >= ansd) return;
	if (x==edx && y==edy) {
		ansd = dep;
		return ;
	}

	int xx, yy;
	int flag;

	rep(i, 0, 4) {
		xx = x;
		yy = y;
		if (judge(xx+dir[i][0], yy+dir[i][1]) || s[xx+dir[i][0]][yy+dir[i][1]] == 1) continue;

		flag = 1;
		while (true) {
			if (judge(xx+dir[i][0], yy+dir[i][1])) {
				flag = 0;
				break;
			}

			if (s[xx+dir[i][0]][yy+dir[i][1]] == 1) {
				flag = -1;
				break;
			}

			xx += dir[i][0];
			yy += dir[i][1];

			if (xx==edx && yy==edy) {
				break;
			}
		}

		if (flag == 0) continue;

		if (flag == -1) {
			s[xx+dir[i][0]][yy+dir[i][1]] = 0;
		}

		dfs(dep+1, xx, yy);

		if (flag == -1) {
			s[xx+dir[i][0]][yy+dir[i][1]] = 1;
		}
	}
}

void solve() {
	init();
	ansd = 11;
	dfs(0, stx, sty);
	if (ansd == 11) ansd = -1;
	printf("%d\n", ansd);
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	// #ifndef ONLINE_JUDGE
		// freopen("data.in", "r", stdin);
		// freopen("data.out", "w", stdout);
	// #endif

	while (scanf("%d %d", &m,&n)!=EOF && (n||m)) {
		rep(i, 0, n)
			rep(j, 0, m)
				scanf("%d", &s[i][j]);
		solve();
	}

	// #ifndef ONLINE_JUDGE
		// printf("time = %ldms.\n", clock());
	// #endif

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, n+1)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define whole(x) (x).begin(),(x).end()
#define rwhole(x) (x).rbegin(), (x).rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define P pair<int, int>
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define eb emplace_back
const ll mod = 1000000007;
const int dx[] = {-1,0,1,0};
const int dy[] = {0,-1,0,1};

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

    string s;
    cin >> s;
    int n = s.size();
    rep(i, n) {
        if (i==0) continue;
        if (s[i]==s[i-1]) {
            cout << i << " " << i+1 << endl;
            return 0;
        }
    }
    rep(i, n) {
        if (i<=1) continue;
        if (s[i]==s[i-2]) {
            cout << i-1 << " " << i+1 << endl;
            return 0;
        }
    }
    cout << "-1 -1" << endl;

    return 0;
}

//vec[i]の範囲外エラー表示
#define _GLIBCXX_DEBUG
//includeとusing
#include <bits/stdc++.h>
using namespace std;
//型名省略
using ll = long long;
using st = string;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
using vc = vector<char>;
using vvc = vector<vector<char>>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using pcc = pair<char, char>;
//定数
const ll INF = 1e9;
const ll MOD = 1e9+7;
const vl dx = {1,0,-1,0,1,0,-1,0};
const vl dy = {0,1,0,-1,0,1,0,-1};
//マクロ
#define rep(i,n) for(ll i = 0; i < (ll) n; i++)
#define rrep(i,n) for(ll i = (ll) n - 1; i >= 0;i--)
#define REP(i,x,n) for(ll i = (ll) x; i < (ll) n; i++)
#define RREP(i,x,n) for(ll i = (ll) n - 1; i >= x; i--)
#define each(i,A) for(ll i: (vl) A)
#define all(A) A.begin(), A.end()
#define Size(A) ll(A.size())
//入力
template<typename T> T input() {T x; cin >> x; return x;}
//正誤判定
void Yes(bool ans){cout << (ans? "Yes" : "No") << endl;}
void YES(bool ans){cout << (ans? "YES" : "NO") << endl;}
//余り切り上げ
ll ceil(ll a,ll b) {return (a + b - 1) / b;}
//最大値・最小値
template <typename T> bool chmax(T &a, const T& b) {if(a < b) {a = b; return true;} return false;}
template <typename T> bool chmin(T &a, const T& b) {if(a > b) {a = b; return true;} return false;}
//最大公約数
ll gcd(ll a,ll b){if(a<b)swap(a,b); if(a%b==0)return b; return gcd(b,a%b);}
//最小公倍数
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
//n乗の余り
ll modPow(ll x,ll n,ll mod){if(n==0)return 1; if(n%2==0)return modPow(x*x,n/2,mod)%mod; return x%mod*modPow(x,n-1,mod)%mod;}

/*--------------------------------------------------------------------------*/

//main関数
int main() {
    st s; cin >> s;
    bool flag = false;
    rep(i,3){
        if(s[i] == '7') flag = true;
    }
    Yes(flag);
}