#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;

using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define repl(i, a, b) for(int i = a; i < int(b); i++)
#define rep(i, n) repl(i, 0, n)
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

int main() {
    int N;
    cin >> N;
    vector<vector<int>> G(N);
    rep(i, N) {
        string s;
        cin >> s;
        rep(j, N) if(s[j] == '1') G[j].push_back(i);
    }

    double ans = 0;

    rep(i, N) {
        vector<bool> seen(N, false);

        auto dfs = [&](auto dfs, int v) -> void {
            if(seen[v]) return;
            seen[v] = true;
            for(int u : G[v]) dfs(dfs, u);
        };

        dfs(dfs, i);

        ans += 1.0 / (double)(count(ALL(seen), true));
    }

    cout << ans << endl;
}

/*
このコード、と～おれ!
Be accepted!
∧＿∧
（｡･ω･｡)つ━☆・*。
⊂　　 ノ 　　　・゜+.
　しーＪ　　　°。+ *´¨)
 　　　　　　　　　.· ´¸.·*´¨) ¸.·*¨)
		  　　　　　　　　　　(¸.·´ (¸.·'* ☆
*/

#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <vector>
#include <numeric>
#include <iostream>
#include <random>
#include <map>
#include <unordered_map>
#include <queue>
#include <regex>
#include <functional>
#include <complex>
#include <list>
#include <cassert>
#include <iomanip>
#include <set>
#include <stack>
#include <bitset>
#include <array>
#include <chrono>

//#pragma GCC target("arch=skylake-avx512")
//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse4")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define repeat(i, n, m) for(int i = n; i < (m); ++i)
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printim(a) printf(a ? "possible\n" : "imposible\n")
#define printdb(a) printf("%.50lf\n", a)
#define printLdb(a) printf("%.50Lf\n", a)
#define printdbd(a) printf("%.16lf\n", a)
#define prints(s) printf("%s\n", s.c_str())
#define all(x) (x).begin(), (x).end()
#define deg_to_rad(deg) (((deg)/360.0L)*2.0L*PI)
#define rad_to_deg(rad) (((rad)/2.0L/PI)*360.0L)
#define Please return
#define AC 0
#define manhattan_dist(a, b, c, d) (abs(a - c) + abs(b - d))

using ll = long long;
using ull = unsigned long long;

constexpr int INF = 1073741823;
constexpr int MINF = -1073741823;
constexpr ll LINF = ll(4661686018427387903);
constexpr ll MOD = 1e9 + 7;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-6;
const long double PI = acosl(-1.0L);

using namespace std;

void scans(string& str) {
	char c;
	str = "";
	scanf("%c", &c);
	if (c == '\n')scanf("%c", &c);
	while (c != '\n' && c != -1 && c != ' ') {
		str += c;
		scanf("%c", &c);
	}
}

void scanc(char& str) {
	char c;
	scanf("%c", &c);
	if (c == -1)return;
	while (c == '\n') {
		scanf("%c", &c);
	}
	str = c;
}

double acot(double x) {
	return PI / 2 - atan(x);
}

ll LSB(ll n) { return (n & (-n)); }

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

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

//cpp_int
#if __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using namespace boost::multiprecision;
#else
using cpp_int = ll;
#endif

//atcoder library
#if __has_include(<atcoder/all>)
#include <atcoder/all>
//using namespace atcoder;
#endif

/*
	random_device seed_gen;
	mt19937 engine(seed_gen());
	uniform_int_distribution dist(1, 100);
*/

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

int main() {

	int n, q;
	scanf("%d%d", &n, &q);
	vector<ll> a(n);
	rep(i, n)scanf("%lld", &a[i]);
	while (q--) {
		ll k;
		scanf("%lld", &k);
		ll ng = 0, ok = (ll)2e18;
		while (abs(ng - ok) > 1) {
			ll mid = (ng + ok) / 2;
			ll idx = distance(a.begin(), upper_bound(all(a), mid));
			if (mid - idx >= k)ok = mid;
			else ng = mid;
		}
		printf("%lld\n", ok);
	}

	Please AC;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;

ll n,k;
ll a[100];

void YPO()
{
    ll len=0,i;
    while(n)
    {
        a[len]=n%10;
        n/=10;
        len++;
    }
    sort(a,&a[len]);
    ll x1=0,x2=0;
    for(i=0;i<len;i++)
    {
        x1*=10;
        x1+=a[i];
    }
    for(i=len-1;i>=0;i--)
    {
        x2*=10;
        x2+=a[i];
    }
    n=x2-x1;
}

int main()
{
    ll i,j;
    scanf("%lld%lld",&n,&k);
    for(i=0;i<k;i++)
    {
        YPO();
    }
    printf("%lld\n",n);
    return 0;
}

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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,x;
    cin>>n>>x;
    int a[n];
for(int i=0;i<n;i++){
    cin>>a[i];
    if(a[i] == x)
    a[i] = 0;
}
for(int i=0;i<n;i++){
    cin>>a[i];
    if(a[i] != 0)
     cout<<a[i]<<" ";
}

}


#include<bits/stdc++.h>
#define int long long
const int N=500050;
using namespace std;

map<int,int> a;
int n,x,s[N],ans;

main(){
	scanf("%lld",&n),a[0]=1;
	for(int i=1;i<=n;i++)scanf("%lld",&x),s[i]=s[i-1]+((i&1)?-x:x),a[s[i]]++;
	for(int i=1;i<=n;i++)--a[s[i]],ans+=a[s[i]];
	printf("%lld",ans);
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INFint = 1e9+1;
const ll INFll = (ll)1e18+1;
const int MOD=1e9+7;

int main(){
  int N,M;
  cin>>N>>M;
  vector<vector<int>> G(N,vector<int>());
  for(int i(0);i<M;i++){
    int a,b; cin>>a>>b; a--;b--;
    G[a].push_back(b); G[b].push_back(a);
  }
  int K; cin>>K;
  vector<int> C(K);
  for(int i(0);i<K;i++){
    cin>>C[i]; C[i]--;
  }
  vector<vector<int>> d(K, vector<int>(N,INFint));
  for(int k(0);k<K;k++){
    queue<int> q;
    d[k][C[k]] = 0;
    q.push(C[k]);
    while(!q.empty()){
      int v = q.front();q.pop();
      for (int next: G[v]){
        if(d[k][next] == INFint){
          d[k][next] = d[k][v] + 1;
          q.push(next);
        }
      }
    }
  }
  int ans(INFint);
  vector<vector<int>> dp(1<<K,vector<int>(K,INFint));
  for(int i(0);i<K;i++) dp[1<<i][i] = 0;
  for(int i(1);i<(1<<K);i++){
    for(int j(0);j<K;j++){
      if(i&(1<<j)){ // iのなかにjが入っている時のみ考える(jが通過していないパターンは矛盾しているのであり得ない)
        for(int k(0);k<K;k++){
          dp[i][j] = min(dp[i][j], dp[i-(1<<j)][k]+ d[k][C[j]]); //集めるdpでkからjにくるパターンを考えている
        }
      }
    }
  }
  for(int i(0);i<K;i++) ans = min(ans, dp[(1<<K)-1][i]);
  if(ans == INFint) cout << -1 << endl;
  else cout << ans+1 << endl;
  return 0;
}

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

#define rep(i,x,y) for(ll i=(x);i<(y);i++)
#define rrep(i,x,y) for(ll i=(ll)(y)-1;i>=(x);i--)
#define all(x) (x).begin(),(x).end()
#define itrout(x) for(int i=0;i<x.size();i++) {cout << x[i] << (i==x.size()-1 ? "\n" : " ");}
#ifdef LOCAL
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl
#define debugbit(x, n) cerr << #x << " = " << bitset<n>(x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl
#define itrdebug(x) cerr << #x << " "; for (auto & el : (x)) {cerr << (el) << " ";} cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define debugbit(x, n)
#define itrdebug(x)
#define dassert(...)
#endif
//#define int long long
//using mint = atcoder::modint;

typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;

signed main(){
    int N, M;
    cin >> N >> M;
    vector<int> A(M), B(M);
    rep(i,0,M) cin >> A[i] >> B[i];
    int K;
    cin >> K;
    vector<int> C(K);
    rep(i,0,K) cin >> C[i];
    vector<vector<int>> edges(N+1);
    rep(i,0,M) {
        edges[A[i]].emplace_back(B[i]);
        edges[B[i]].emplace_back(A[i]);
    }
    vector<vector<int>> distances(K, vector<int>(K, INT_MAX));
    rep(i,0,K) {
        vector<int> distances_(N+1, INT_MAX);
        priority_queue<pair<int, int>, vector<pair<int,int>>, greater<>> q;
        q.push({0, C[i]});
        distances_[C[i]] = 0;
        while (!q.empty()) {
            auto [cost, current] = q.top();
            q.pop();
            for (const auto& next : edges[current]) {
                if (distances_[next] > cost + 1) {
                    distances_[next] = cost + 1;
                    q.push({distances_[next], next});
                }
            }
        }
        rep(j,0,K) {
            if (distances_[C[j]] == INT_MAX) {
                cout << -1 << endl;
                return 0;
            }
            distances[i][j] = distances_[C[j]];
        }
    }
    vector<vector<int>> dp(1 << K, vector<int>(K, INT_MAX));
    rep(i,0,K) dp[1<<i][i] = 1;
    rep(i,1,1<<K) rep(j,0,K) {
        if(dp[i][j] == INT_MAX) continue;
        rep(k,0,K) {
            dp[i | 1<<k][k] = min(dp[i | 1<<k][k], dp[i][j] + distances[j][k]);
        }
    }
    cout << *min_element(all(dp[(1<<K) - 1])) << endl;
    return 0;
}


#include<iostream>
#include<vector>

using namespace std;
using ll = long long;

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

	vector<int> A(n);
	for (auto& Ai : A) cin >> Ai;

	ll ans = (ll)2e18;
	for (int base = 1; base <= n; base++) {
		vector<vector<int>> T(base + 1, vector<int>(base, -(int)2e9));
		T[0][(base-(x%base))%base] = 0;
		for (auto ind : A) {
			for (int i = base - 1; i >= 0; i--) {
				for (int j = 0; j < base; j++) {
					if (T[i][j] + ind > T[i + 1][(j + ind) % base]) {
						T[i + 1][(j + ind) % base] = T[i][j] + ind;
					}
				}
			}
		}
		if (T[base][0] < 0) continue;
		ans = min(ans, (x - T[base][0]) / base);
	}
	cout << ans << '\n';

}

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef long double ld;
#define REP(i, n) for(ll i = 0LL; i < (ll)(n); i++)
#define REPR(i, n) for (ll i = (ll)(n) - 1; i >= 0; i--)
#define FOR(i, n, m) for(ll i = (ll)n; i < (ll)(m); i++)
#define ALL(x) (x).begin(),(x).end()
#define MOD 1000000007
#define INF (1LL << 62)

#define PI (acos(-1))
#define PRINT(x) std::cout << x << endl

ll gcd(ll a, ll b) { return b ? gcd(b,a%b) : a;}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll countDigit(ll n) { return floor(log10(n) + 1); }
typedef pair <ll,ll> P;
static const ll dx[8] = {0,1,0,-1, 1, -1, 1, -1}, dy[8] = {1,0,-1,0,1,1,-1,-1};

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    for (int i = 0; i < (int)v.size(); ++i) os << v[i] << " ";
    return os;
}

template <typename T1, typename T2>
ostream& operator<<(ostream& os, const map<T1, T2>& m)
{
    for (auto p : m) os << "<" << p.first << ", " << p.second << "> ";
    return os;
}

int main()
{
  ll N, K;
  cin >> N >> K;
  vector<vector<ll>> mat(N, vector<ll>(N, 0));
  REP(i, N) {
    REP(j, N){
      cin >> mat[i][j];
    }
  }

  vector<ll> indices(N-1);
  iota(indices.begin(), indices.end(), 1);
  ll cnt = 0;
  do {
      ll sum = mat[0][indices[0]];
      REP(i, N-2) {
        sum += mat[indices[i]][indices[i+1]];
      }
      sum += mat[indices[N-2]][0];
      if (K == sum) cnt++;
  } while (std::next_permutation(indices.begin(), indices.end()));
  cout << cnt << endl;
  return 0;
}


#include<bits/stdc++.h>
#define show(x) cerr << #x << " = " << x << '\n';
using namespace std;
#define int long long
#pragma GCC optimize(2)

int n, q;
vector<int> a;
void solve()
{
    cin >> n >> q;
    for(int i = 1; i <= n; i ++)
    {
        int t;
        cin >> t;
        a.push_back(t);
    }
    for(int i = 1; i <= q; i ++)
    {
        int m;
        cin >> m;
        int l = 0, r = 2e18;
        while(l < r)
        {
            int mid = l + r >> 1;
            int p = upper_bound(a.begin(), a.end(), mid) - a.begin() - 1;
            int x = mid - p;
            if(x <= m)
            {
                l = mid + 1;
            }
            else
            {
                r = mid;
            }
        }
        cout << l << '\n';
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    solve();
}

/*NEVER GIVE UP*/
/*First try brute force and check if it is in the range of time complexity*/
/*Then try to solve the problem in mathematical form*/
/*Then try to imagine how the final ans will look like*/
/*Write on paper if still not able to get*/
/*Try to think diff. algorithms and data structures that could solve the problem*/
#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y, long long p)
{
    long long res = 1;     // Initialize result

    x = x % p; // Update x if it is more than or
                // equal to p

    if (x == 0) return 0; // In case x is divisible by p;

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;

        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
int main()
{long long n=0,q=0,d=0,k=0;
    cin>>n>>q;
    long long a[n];vector<long long> dp(n);
    for(long long j=0;j<n;j++)
    cin>>a[j];
    sort(a,a+n);
    dp[0]=a[0]-1;
    for(long long j=1;j<n;j++)
    {
        dp[j]=dp[j-1]+(a[j]-a[j-1]-1);
    }
    for(long long j=0;j<q;j++)
    {
        cin>>k;
        d=lower_bound(dp.begin(),dp.end(),k)-dp.begin();
        if(d==n)
        cout<<a[n-1]+(k-dp[n-1])<<endl;
        else if(d==0)
        cout<<k<<endl;
        else
        {
            cout<<a[d]-dp[d]+k-1<<endl;
        }
    }
}


// ＵＴＦ−８

#include<bits/stdc++.h>

using namespace std;
using ll = long long int;
using lc = complex<double>;

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

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

int main(void) {
    constexpr ll MOD = 1 ? 1e9+7 : 998244353;
    const double PI = acos(-1);
    constexpr double eps = 1e-10;
    cout << fixed << setprecision(32);
    cin.tie(0); ios::sync_with_stdio(false);

    if(1) {
        ll n;
        cin >> n;
        lc p0, p1;
        {
            double x0, y0, x1, y1;
            cin >> x0 >> y0 >> x1 >> y1;
            p0 = lc(x0, y0);
            p1 = lc(x1, y1);
        }
        lc c = (p0 + p1) / 2.0;

        lc res = (p0 - c) * lc(cos(2 * PI / n), sin(2 * PI / n)) + c;
        cout << real(res) << ' ' << imag(res) << endl;
    }
    return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <complex>

using namespace std;
using ll = long long;
using C = complex<double>;

C inC() {
    double x, y;
    cin >> x >> y;
    return C(x, y);
}

int main() {
    int n;
    cin >> n;
    C s = inC();
    C t = inC();

    C o = (s+t)/2.0;

    double pi = acos(-1);
    double rad = 2 * pi / n;
    C r(cos(rad), sin(rad));

    C ans = o + (s - o) * r;
    printf("%.10f %.10f\n", ans.real(), ans.imag());
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fast ios_base::sync_with_stdio(false);  cin.tie(NULL); cout.tie(NULL);
#define scan(a,n) for(long long int index=0;index<n;index++) cin>>a[index];
#define print(a,n) for(long long int index=0;index<n;index++) cout<<a[index]<<" ";cout<<endl;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define vli vector<long long int>
#define vlli vector<pair<long long int,long long int>>
#define vsi vector<string>
#define all(n) n.begin(),n.end()
#define forn(i,a,b) for(i=a;i<b;i++)
ll mul(ll x,ll y){  return (x*y)%mod;}
ll power(ll x, ll y) {ll res = 1; x %= mod;
 while (y) {if (y & 1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;}
ll mod_inv(ll x) {return power(x, mod - 2);}
ll fact(ll n) {ll res = 1; for (ll i = 2; i <= n; i++) res = mul(res , i); return res; }
ll nCr(ll n, ll r) { return mul(fact(n),mod_inv(mul(fact(r),fact(n - r))));}

int main() {
  fast;
  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif
  ll t=1;
  // cin>>t;
  while(t--){
    ll a,b,c,d;

    cin>>a>>b>>c>>d;
    cout<<max(a,b)-min(c,d)<<endl;

  }
return 0;
 }

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

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int a, b, c, d, maks=-1e9+7; cin >> a >> b >> c >> d;
	for(int i=a;i<=b;++i){
		for(int j=c;j<=d;++j){
			maks=max(maks, i-j);
		}
	}
	cout << maks << '\n';
}
/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
*/


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

struct Edge{
  int v,c,d;
  inline ll arrive(ll t){
    return t+c+ll(d)/(t+1);
  }
  inline ll tmin(ll t0){
    return arrive(max(t0,ll(round(sqrt(d)))-1));
  }
};

int main(){
  int n,m;
  cin>>n>>m;
  vector<vector<Edge>> adj(n);
  for(int i=0;i<m;i++){
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    a--;
    b--;
    adj[a].push_back({b,c,d});
    adj[b].push_back({a,c,d});
  }
  priority_queue<pair<ll,int>> pq;
  vector<ll> dist(n,-1);
  pq.push({0,0});
  while(!pq.empty()){
    int u=pq.top().second;
    ll t0=-pq.top().first;
    pq.pop();
    if(dist[u]!=-1)continue;
    dist[u]=t0;
    for(auto &e:adj[u])
      if(dist[e.v]==-1)
        pq.push({-e.tmin(t0),e.v});
  }
  cout<<dist[n-1]<<endl;
}

//wtrl,everybody hangbeat me
#include<bits/stdc++.h>
/*#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>*/
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef pair<ll,ll> pll;
/*=====================================================================*/
#define pb push_back
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(s) (s).begin(),(s).end()
#define repd(i,n) for(int i=n-1;i>=0;i--)
#define forn(i,p,n) for(int i=p;i<=n;i++)
#define ford(i,p,n) for(int i=n;i>=p;i--)
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();++i)
#define INF 1e9
#define PI acos(-1)
/*=====================================================================*/
string int_to_string(ll n)
{
	string s="";
	while(n)
	{
		ll now=n%10;
		s+=now+'0';
		n/=10;
	}
	reverse(s.begin(),s.end());
	return s;
}
ll string_to_int(string s)
{
	ll n=0;
	rep(i,s.size())
	{
		n*=10;
		n+=s[i]-'0';
	}
	return n;
}
/*======================================================================*/
const int dx[]={-1,0,1,0};
const int dy[]={0,-1,0,1};
const int month[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
struct node
{
	ll to;
	ll c;
	ll d;
};
vector<node> v[100010];
ll dist[100010];
bool use[100010];
inline ll calc(ll down,ll up)
{
	ll res=max(down,(ll)sqrt((ll)up));
	while(up/(res+1)>up/(res+2))
	{
		res++;
	}
	return res-down+up/(res+1);
}
void dijkstra(ll now)
{
	priority_queue<pll> pq;
	pq.push(pll((ll)0,(ll)now));
	dist[now]=(ll)0;
	while(!pq.empty())
	{
		pll now=pq.top();
		pq.pop();
		ll nd=now.second;
		if(use[nd]==1)
		{
			continue;
		}
		use[nd]=1;
		rep(i,v[nd].size())
		{
			ll cost=v[nd][i].c+calc(dist[nd],v[nd][i].d);
			//cout<<cnt<<endl;
			if(dist[nd]+cost>=dist[v[nd][i].to])
			{
				continue;
			}
			dist[v[nd][i].to]=cost+dist[nd];
			pq.push(pll(-dist[v[nd][i].to],(ll)v[nd][i].to));
		}
	}
}
void solve()
{
	int n,m;
	cin>>n>>m;
	rep(i,m)
	{
		ll a,b,c,d;
		scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
		v[a].pb({b,c,d});
		v[b].pb({a,c,d});
	}
	forn(i,1,n)
	{
		dist[i]=1e18;
	}
	dijkstra(1);
	printf("%lld",dist[n]==1e18?-1:dist[n]);
}
/*======================================================================*/
int main()
{
   // std::ios::sync_with_stdio(false);
  /*
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
  */
  /*====================================================================*/
  int t=1;
  //cin>>t;
  while(t--)
  {
  	solve();
  }
  return 0;
}
/*
注意数组越界，vector长度为0的时候循环会RE
注意输入顺序
注意循环时int*int爆ll
注意答案存爆ll
*/


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

#define MOD (long long)(1e9+7)
#define INF (1LL<<60)
#define rep(i,n) for(ll i = 0; i < (n); i++)
#define rep1(i,n) for(ll i = 1; i <= (n); i++)

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

// 最大公約数
ll gcd(ll a, ll b)
{
  if(b == 0) return a;
  return gcd(b, a % b);
}

// mod m におけるa の逆元
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b; swap(a, b);
    u -= t * v; swap(u, v);
  }
  u %= m;
  if (u < 0) u += m;
  return u;
}

// 素因数分解
vector<pair<ll, ll>> prim;
void pf(ll n)
{
  ll s = sqrt(n);
  ll r = 0;
  for(ll i = 2; i <= s; i++) {
    if((n % i) == 0) {
      r = 0;
      do {
	r++;
	n = n / i;
      } while((n % i) == 0);
      prim.push_back({i, r});
    }
  }
  if(n > s) {
    prim.push_back({n, 1});
  }
}

void solve()
{

  ll a, b, c, d; cin >> a >> b >> c >> d;
  cout << (b -c) << "\n";

}

int main(void)
{
  // ll t; cin >> t; rep(i, t)
  solve();
}

// execute g++ main.cpp -std=c++14 -I C:\Users\naoya\Desktop\code\Atcoder
#include<bits/stdc++.h>
//#include<atcoder/all>
typedef long long ll;
typedef long double ld;
using namespace std;
//using namespace atcoder;

using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

//ordered_set 重複不可
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// use set_function + find_by_order(select itr-num)

#define REP(i, l, n) for(int i=(l), i##_len=(n); i<i##_len; ++i)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back

ll gcd(ll a,ll b){return b ? gcd(b,a%b) : a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

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;}

char alpha[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char Alpha[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

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

//cout << std::fixed << std::setprecision(15) << y << endl; //小数表示

ll rtime(ll now, ll cost, ll dcost){
    auto f = [&](double t) {
        return (double)t + (double)cost + ((double)dcost/(double)(t+1));
    };
    ll st = now;
    double left = now, right = 5e17;
    int cnt = 100;
    while (cnt--) {
        if(abs(right - left) < 0.5) break;
        double c1 = ((double)left * 2 + (double)right) / 3;
        double c2 = ((double)left + (double)right * 2) / 3;

        // もしf(c2)のほうが良い(小さい)なら、駄目な方lowを更新する
        if (f(c1) > f(c2)) left = c1;
        else right = c2;
    }
    auto f2 = [&](ll t) {
        return t + cost + (dcost/(t+1));
    };
    ll k = (ll)right;
    ll kk = max((ll)left, k+1);
    ll kkk = max(st, k-1);
    return min({f2(k), f2(kk), f2(kkk)});
}

///////////////////////////////////////////////////////////////
struct edge{ll to, cost, dcost;};

class Dijkstra{
    public:
        vector<vector<edge>> G;
        vector<ll> d;

        Dijkstra(int n){
            G = vector<vector<edge> >(n);
            d = vector<ll>(n, 2e18);
        }

        void add_edge(ll from, ll to, ll cost, ll dcost){
            edge e = {to, cost, dcost};
            G[from].pb(e);
        }

        void dijkstra(ll s){
            priority_queue<Pll, vector<Pll>, greater<Pll> > que;
            d[s] = 0;
            que.push(Pll(0, s));

            while(!que.empty()){
                Pll p = que.top(); que.pop();
                ll v = p.second;
                if(d[v] < p.first) continue;
                for(int i = 0; i < G[v].size(); i++){ //二部探索でどこまで待つか考える
                    edge e = G[v][i];
                    ll cc = rtime(min(d[v], p.first), e.cost, e.dcost);
                    if(d[e.to] > cc){
                        d[e.to] = cc;
                        que.push(Pll(d[e.to], e.to));
                    }
                }
            }
        }
};

int main(){
    int n, m; cin >> n >> m;
    Dijkstra dijk(n);
    REP(i,0,m){
        ll a, b, c, d; cin >> a >> b >> c >> d;
        a--; b--;
        dijk.add_edge(a,b,c,d);
        dijk.add_edge(b,a,c,d);
    }
    dijk.dijkstra(0);
    if(dijk.d[n-1] == 2e18){
        cout << -1 << endl;
        return 0;
    }
    cout << dijk.d[n-1] << endl;
}


#include <bits/stdc++.h>
#define LL long long
#define rep(i,n) for(int i=0;i<n;++i)
#define repn(i,n) for(int i=1;i<=n;++i)
using namespace std;
int qmi(int a, int k, int p)  // 快速幂模板
{
    int res = 1 % p;
    while (k)
    {
        if (k & 1) res = (LL)res * a % p;
        a = (LL)a * a % p;
        k >>= 1;
    }
    return res;
}

int C(int a, int b, int p)  // 通过定理求组合数C(a, b)
{
    if (a < b) return 0;

    LL x = 1, y = 1;  // x是分子，y是分母
    for (int i = a, j = 1; j <= b; i --, j ++ )
    {
        x = (LL)x * i % p;
        y = (LL) y * j % p;
    }

    return x * (LL)qmi(y, p - 2, p) % p;
}

int lucas(LL a, LL b, int p)
{
    if (a < p && b < p) return C(a, b, p);
    return (LL)C(a % p, b % p, p) * lucas(a / p, b / p, p) % p;
}
const int N = 1e6+10;
int b[N];
int main()
{
	int n; cin>>n;
	string s; cin>>s;
	for(int i=0;i<n;i++) b[i] = lucas(n-1,i,3);
	//for(int i=0;i<n;i++) cout<<b[i]<<' ';
	 //B 0 W 1 R 2

		if(n&1)
		{
			int ans = 0;
			for(int i=0;i<=n;i++)
			{
				if(s[i]=='W') ans += 1 * b[i];
				if(s[i]=='R') ans += 2 * b[i];
			}
			ans %= 3;
			if(ans==2) cout<<"R"<<endl;
			else if(ans==1) cout<<"W"<<endl;
			else cout<<"B"<<endl;
		}else{
			int ans = 0;
			for(int i=0;i<n;i++)
			{
				if(s[i]=='W') ans -= 1 * b[i];
				if(s[i]=='R') ans -= 2 * b[i];
			}
			ans %= 3;
			ans +=3 ;
			if(ans==2) cout<<"R"<<endl;
			else if(ans==1) cout<<"W"<<endl;
			else cout<<"B"<<endl;
		}
}

#include<iostream>
#include<unordered_map>
#include<cstring>
using namespace std;
const int N = 400010;
typedef long long ll;

int m, q;
ll n;

int pow10(ll n)
{
	ll res = 1%q;
	int i = -1;
	while(n >> ++i);
	--i;
	while(~i)
	{
		int j = n >> i & 1;
		res = (res * res) % q;
		if(j) res = (res * 10) % q;
		--i;
	}
	return res;
}

int main()
{
	cin >> n >> m;
	q = m * m;
	printf("%d", pow10(n) / m % m);
}


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

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

    vector<P> wv(n);
    rep(i, n) {
        int w, v;
        cin >> w >> v;
        wv[i] = {v, -w};
    }
    sort(wv.begin(), wv.end(), greater<>());
    vector<ll> x(m);
    rep(i, m) cin >> x[i];

    rep(i, q) {
        int l, r;
        cin >> l >> r;
        l--, r--;
        multiset<ll> s;
        rep(j, m) {
            if (j >= l && j <= r) continue;
            s.insert(x[j]);
        }

        ll res = 0;
        rep(j, n) {
            auto it = s.lower_bound(-wv[j].second);
            if (it == s.end()) continue;
            res += wv[j].first;
            s.erase(it);
        }
        cout << res << endl;
    }
}

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

class segtree{
public:
	static const int N=1<<18;
	ll dp[1<<19];
	segtree(){
		memset(dp,0,sizeof(dp));
	}
	void update(int k,ll v){
		k+=N-1;
		dp[k]+=v;
		while(k>0){
			k=(k-1)/2;
			dp[k]=dp[k*2+1]+dp[k*2+2];
		}
	}

	ll query(int a,int b,int k=0,int l=0,int r=N){
		if(b<=l || r<=a)return 0;
		if(a<=l && r<=b)return dp[k];
		int mid=(l+r)/2;
		ll vl=query(a,b,k*2+1,l,mid);
		ll vr=query(a,b,k*2+2,mid,r);
		return vl+vr;
	}
};

segtree sumx,cntx;
segtree sumy,cnty;
int n,m,q;
ll sum;
ll a[200005];
ll b[200005];
int t[200005],x[200005];
ll y[200005];
vector<ll> yi;

int main(void){
	scanf("%d%d%d",&n,&m,&q);
	cntx.update(0,n);
	cnty.update(0,m);
	yi.push_back(0);

	for(int i=0;i<q;i++){
		scanf("%d%d%lld",&t[i],&x[i],&y[i]);
		x[i]--;
		yi.push_back(y[i]);
	}
	sort(yi.begin(),yi.end());
	yi.erase(unique(yi.begin(),yi.end()),yi.end());

	for(int i=0;i<q;i++){
		int pos=lower_bound(yi.begin(),yi.end(),y[i])-yi.begin();
		if(t[i]==1){
			ll val_mi=sumy.query(a[x[i]],yi.size()+1);
			ll cnt_mi=cnty.query(0,a[x[i]]);
			sum-=cnt_mi*yi[a[x[i]]]+val_mi;
			sumx.update(a[x[i]],-yi[a[x[i]]]);
			cntx.update(a[x[i]],-1);
			a[x[i]]=pos;
			sumx.update(a[x[i]],yi[a[x[i]]]);
			cntx.update(a[x[i]],1);
			val_mi=sumy.query(a[x[i]],yi.size()+1);
			cnt_mi=cnty.query(0,a[x[i]]);
			sum+=cnt_mi*yi[a[x[i]]]+val_mi;
			//printf("%lld %lld\n",val_mi,cnt_mi);
		}else{
			ll val_mi=sumx.query(b[x[i]],yi.size()+1);
			ll cnt_mi=cntx.query(0,b[x[i]]);
			sum-=cnt_mi*yi[b[x[i]]]+val_mi;
			sumy.update(b[x[i]],-yi[b[x[i]]]);
			cnty.update(b[x[i]],-1);
			b[x[i]]=pos;
			sumy.update(b[x[i]],yi[b[x[i]]]);
			cnty.update(b[x[i]],1);
			val_mi=sumx.query(b[x[i]],yi.size()+1);
			cnt_mi=cntx.query(0,b[x[i]]);
			sum+=cnt_mi*yi[b[x[i]]]+val_mi;
		}
		printf("%lld\n",sum);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+5;
typedef long long LL;
int n,m,q,a[maxn],b[maxn];
LL ans;
struct Seg {
	LL sum[maxn<<5];
	int root,ch[maxn<<5][2],cnt[maxn<<5],tot;
	void upd(int &u,int l,int r,int p,int v) {
		if(!u)u = ++tot;
		cnt[u]+=v,sum[u] += v*p;
		if(l == r)return ;
		int mid = l+r>>1;
		p<=mid?upd(ch[u][0],l,mid,p,v):upd(ch[u][1],mid+1,r,p,v);
	}
	int Cnt(int u,int l,int r,int x,int y){
		if(!u||l>y||r<x)return 0;
		if(l>=x&&r<=y)return cnt[u];
		int mid = l+r>>1;
		return Cnt(ch[u][0],l,mid,x,y)+Cnt(ch[u][1],mid+1,r,x,y);
	}
	LL Sum(int u,int l,int r,int x,int y){
		if(!u||l>y||r<x)return 0;
		if(l>=x&&r<=y)return sum[u];
		int mid = l+r>>1;
		return Sum(ch[u][0],l,mid,x,y)+Sum(ch[u][1],mid+1,r,x,y);
	}
}t1,t2;
int main() {
	scanf("%d %d %d",&n,&m,&q);
	for(int i=1;i<=n;++i)t1.upd(t1.root,0,1e8,0,1);
	for(int i=1;i<=m; ++i)t2.upd(t2.root,0,1e8,0,1);
	while(q--) {
		int ty,x,y,les;
		scanf("%d %d %d",&ty,&x,&y);
		if(ty == 1){
			les = t2.Cnt(t2.root,0,1e8,0,a[x]);
			ans -= t2.Sum(t2.root,0,1e8,a[x]+1,1e8)+1ll*les*a[x];

			t1.upd(t1.root,0,1e8,a[x],-1),a[x] = y,t1.upd(t1.root,0,1e8,a[x],1);

			les = t2.Cnt(t2.root,0,1e8,0,a[x]);
			ans += t2.Sum(t2.root,0,1e8,a[x]+1,1e8)+1ll*les*a[x];
		}
		if(ty == 2){
			les = t1.Cnt(t1.root,0,1e8,0,b[x]);
			ans -= t1.Sum(t1.root,0,1e8,b[x]+1,1e8)+1ll*les*b[x];

			t2.upd(t2.root,0,1e8,b[x],-1),b[x] = y,t2.upd(t2.root,0,1e8,b[x],1);

			les = t1.Cnt(t1.root,0,1e8,0,b[x]);
			ans += t1.Sum(t1.root,0,1e8,b[x]+1,1e8)+1ll*les*b[x];
		}
		printf("%lld\n",ans);
	}
	return 0;
}

/*                             हरे कृष्ण हरे कृष्ण कृष्ण कृष्ण हरे हरे
                                  हरे राम हरे राम राम राम हरे हरे                                     */
#include<bits/stdc++.h>
using namespace std;
#define pb emplace_back
#define pob pop_back
typedef long long int lli;
#define test lli t; cin>>t; while(t--)
#define ff first
#define ss second
#define F(n) for(lli i=0;i<n;i++)
#define pf pop_front
#define lb lower_bound
#define ub upper_bound
#define setbits(x)  __builtin_popcountll(x)
#define zerobits(x)  __builtin_ctzll(x)
#define bs binary_search
#define all(x) x.begin(),x.end()
#define nl "\n"
#define loop(i,s,n) for(lli i=s;i<n;i++)
#define pp(a)  for(auto x : a)  cout<<x<<" ";  cout<<nl;
#define mem(arr,x) memset(arr,x,sizeof(arr))
#define mod 1000000007
#define inf 1e18
#define tt(n) cin>>n; lli a[n]; loop(i,0,n) cin>>a[i];
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ps(x,y) fixed<<setprecision(y)<<x
lli solve(lli n)
{
 lli r,i;
 while(n!=0)
 {
     r=n%10;
     if(r==7) return -1;
     n/=10;
 }
 return 0;
}
lli decToOctal(lli n)
{
    lli octalNum[100];
    lli i = 0,f=0;
    while (n != 0) {
        octalNum[i] = n % 8;
        n = n / 8;
        i++;
    }
    for (lli j = i - 1; j >= 0; j--)
        {
            if(octalNum[j]==7)
            {
                f=1;
                break;
            }
        }
        if(f) return -1;
        else return 0;
}
int main()
{
fast;
 	lli n,i,j,c,x=0;
 	cin>>n;
 	lli a[n],s=0;
 	loop(i,0,n) cin>>a[i];
 	sort(a,a+n);
 	loop(i,0,n)
 	{
 	    s+=(i*a[i])-((n-1-i)*a[i]);
 	}
 	cout<<s;
return 0;
}


/*
“The only way that we can live is if we grow.
 The only way we can grow is if we change.
The only way we can change is if we learn.
 The only way we can learn is if we are exposed.
 And the only way that we are exposed is if we throw ourselves into the open.”
 ***************************************   ************************   **********************************
*/
#include <bits/stdc++.h>
using namespace std;

//---------------------mishra ka idea------------------
#define int         long long
#define double      long double
#define pb          push_back
#define pf          push_front
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(),(a).rend()
#define x           first
#define y           second
#define endl        '\n'
#define sz(x)	    (int)(x).size()
#define ull       unsigned long long
//------------------------mishra ka idea-------------------

//-------------------------alsi hu mai-----------------------
#define fo(i,l,u)   for(i=l;i<u;i++)
#define rfo(i,l,u)  for(i=l;i>=u;i--)
#define allfo(s)    for(auto it=(s).begin();it!=(s).end();it++)
#define _init(b)     memset(b,-1,sizeof(b))
#define _init0(b)     memset(b,0,sizeof(b))
#define MOD         1000000007
#define hell        998244353
#define output(x) cout << (x ? "YES" : "NO")<<endl;
#define Output(x) cout << (x ? "Yes" : "No")<<endl;
//-------------------------alsi hu mai-----------------------

//--------------------het se uthaya---------------------
#define MOD2 (998244353)
#define MOD3 (1000000009)
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)

template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
ifstream cinn("in.txt");ofstream coutt("out.txt");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}

//--------------------het se uthaya-------------------------------------------------------------------

int gcd(int a, int b) {if(a>b)swap(a,b) ;  if (a == 0)  return b; return gcd(b % a, a); }
int rint(int  l , int r){return rand()%(r-l+1) + l ;}

//---------------------------aab code dekho bahut template dekh liya---------------------------------
bool poss(int a , int b){
	return (a<b && a>-b);
}

signed main()
{
   // freopen("input.txt", "r", stdin);
   // freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
   int i,j,x,n,k;
   cin>>n>>k;
   map<int,int> mp;
   mp[k]=1;
   vi v;

   fo(i,0,n){
   	cin>>x;
   	v.pb(x);
   }
   v.pb(INF);
   reverse(all(v));
   int a , b , c;

   fo(i,1,n){
   		map<int,int>  np;
   		for(auto aa : mp){
   			// cout<<">>"<<aa.x<<","<<aa.y<<"<< ";
   				a = aa.x/v[i];
   				if(poss(aa.x-a*v[i],v[i]) && poss(a,v[i-1]/v[i]))np[aa.x-a*v[i]]+=aa.y;
   				if(poss(aa.x-(a+1)*v[i],v[i]) && poss(a+1,v[i-1]/v[i]) )np[aa.x-(a+1)*v[i]]+=aa.y;
   				if(poss(aa.x-(a-1)*v[i],v[i]) && poss(a-1,v[i-1]/v[i]))np[aa.x-(a-1)*v[i]]+=aa.y;

   		}
   		//cout<<endl;
   		swap(np,mp);
   }
   int ans=0;
   for(auto aa : mp){
   		//cout<<">>"<<aa.x<<","<<aa.y<<"<< ";
   		ans+=aa.y;
   }
   cout<<ans<<endl;

    return 0;
}

// Problem: F - Valid payments
// Contest: AtCoder - AtCoder Beginner Contest 182
// URL: https://atcoder.jp/contests/abc182/tasks/abc182_f
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define ll long long int
#define ii int
#define du double
#define jmp "\n"
#define vl vector<ll>
#define vvl vector<vl>
#define pb push_back
#define printv(v)     \
  for (auto x : v)    \
    cout << x << " "; \
  cout << jmp;
#define vi vector<int>
#define vb vector<bool>
#define ump unordered_map
#define SORT(v) sort(v.begin(), v.end())
#define REV(x) reverse(x.begin(), x.end())
#define all(x) x.begin(), x.end()
#define SET(x, a) memset(x, a, sizeof(x))
#define si(x) (ll) x.size()
#define ff first
#define ss second
#define I(a)        \
  for (auto &x : a) \
    cin >> x;
#define iin insert
#define deb(x) cout << "test " << #x << "=" << x << jmp
#define deb2(x, y) cout << "test " << #x << "=" << x << "," << #y << "=" << y << jmp
using namespace std;
const ll nax = 1e5 + 5;
vl a;
ll n, k;
map<pair<ll, ll>, ll> dp;
ll r(ll x, ll i)
{

  if (i == n - 1)
    return (x % a[i] == 0);

  if (dp.count({i, x}))
    return dp[{i, x}];
  if (x % a[i + 1])
    return dp[{i, x}] = r(x - (x % a[i + 1]), i + 1) + r(x + a[i + 1] - (x % a[i + 1]), i + 1);
  else
    return dp[{i, x}] = r(x, i + 1);
}
void solve()
{
  // ll n, k;
  cin >> n >> k;
  a = vl(n);
  I(a);
  cout << r(k, 0);
}
int32_t main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}


#define _DEBUG
#include "bits/stdc++.h"
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#ifdef _DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(index,num) for(int index=0;index<(int)num;index++)
#define rep1(index,num) for(int index=1;index<=(int)num;index++)
#define brep(index,num) for(int index=(int)num-1;index>=0;index--)
#define brep1(index,num) for(int index=(int)num;index>0;index--)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-7
#define mp(a1,a2) make_pair(a1,a2)
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
#define puba(a) emplace_back((a))
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
ll INFl=(ll)1e+18+1;
int INF=1e+9+1;
int main(){
	ll N,M,A[200001];
	scan(N>>M);
	if(M==0){
		prin(1);
		return 0;
	}
	else if(M==N){
		prin(0);
		return 0;
	}
	rep(i,M){
		scan(A[i]);
	}
	sort(A,A+M);
	vll sa;
	if(A[0]!=1) sa.puba(A[0]-1);
	rep(i,M-1){
	//	debug(i,A[i]);
		if(A[i+1]-A[i]>=2){
			sa.puba(A[i+1]-A[i]-1);
		}
	}
	//debug(A[M-1]);
	if(A[M-1]!=N) sa.puba(N-A[M-1]);
	sort(ALL(sa));
	ll K=sa[0];
	//debug(K);
	ll ans=0;
	for(const auto& s:sa){
	//	debug(s);
		ans+=s/K+(s%K!=0);
	}
	prin(ans);
	return 0;
}

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

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

  int count = 0;

  int X;
  double A = 1.0000/3.000;
  X = pow(N,A)+0.00001;

  int Y,Z;

  for(int i = 0; i < X ;i++){
    Y = sqrt(N/(i+1));
    //cout << Y << endl;
    for(int j = i; j < Y ;j++){
      Z = N/(i+1)/(j+1);
      //cout << i << "F" << j << "F"<<  Z << endl;
      for(int k = j;k < Z ; k++){
        //cout << i+1 << " " << j+1 << " " << k+1 << endl;
        if(i!=j && j!=k && k!=i)count +=6;
        else if(i==j && j==k)count++;
        else count +=3;
      }

    }
  }
  cout<< count << endl;
}


#include <bits/stdc++.h>
#define pb push_back
#define ss second
#define ff first
#define all(x) x.begin(), x.end()
#define INF 3000000000000001 // 3e15
#define ll_max 9000000000000000000
#define PI 3.14159265358979323846L
#define fill(a, n, x) for(int i = 0; i < n; i++) a[i] = x;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll mod = 1000000007;
const ll N = 100100;
const ll M = log2(N) + 1;

struct point
{
    ll x, y;
};

template<typename T>
T fast_exp(T x,T n)
{
    if(n == 0) return 1;
    T ans = 1;
    if(n % 2 == 0)
    {
         ans = fast_exp(x, n / 2);
         ans = ans * ans % mod;
    }
    else ans = fast_exp(x, n - 1) * x % mod;
    return ans;
}

struct node {
    int vertex;
    bool mx;
    bool mn;
    node(int v, bool a, bool b)
    {
        vertex = v;
        mx = a;
        mn = b;
    }
};

struct cmp {
    bool operator () (pair<ll, node> a, pair<ll, node> b)
    {
        if(a.ff < b.ff)
            return 1;
        return 0;
    }
};

ll fact[N], infact[N];
void work()
{
    fact[0] = fact[1] = infact[0] = infact[1] = 1;
    for(ll i = 2; i < N; i++)
    {
        fact[i] = fact[i - 1] * i % mod;
        infact[i] = fast_exp(fact[i], mod - 2);
    }
}

ll ncr(ll n, ll r)
{
    return fact[n] * infact[n - r] % mod * infact[r] % mod;
}

int main()
{
    fast;
    int _ = 1;
    //cin >> _;
    work();
    int test = 0;
    while(_--)
    {
        test++;
        ll n, m, q, x = 0, y = 0, ans = 0, k = 0;
        cin >> n;
        x = 0, y = 0;
        ll a[n + 1], b[n + 1];
        a[0] = 0;
        for(int i = 1; i <= n; i++) cin >> a[i];
        for(int i = 1; i <= n; i++) cin >> b[i];
        for(int i = 1; i <= n; i++)
        {
            a[i] = max(a[i - 1], a[i]);
            ans = max(a[i] * b[i], ans);
            cout << ans << "\n";
        }
    }
    return 0;
}

//Author: AnandRaj doubleux
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pll> vpll;

#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define test() int t;cin>>t;while(t--)
#define all(v) v.begin(),v.end()
#define prinp(p) cout<<p.first<<" "<<p.second<<endl
#define prinv(V) for(auto v:V) cout<<v<<" ";cout<<endl
#define take(V,f,n) for(int in=f;in<f+n;in++) cin>>V[in]
#define what(x) cerr<<#x<<" = "<<x<<endl
#define KStest() int t,t1;cin>>t;t1=t;while(t--)
#define KScout cout<<"Case #"<<t1-t<<": "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7,MAX = 1e6+5;
const ll INF = 1e18+5;

int main()
{
	int n;
	cin>>n;
	vi A(n),B(n);
	take(A,0,n);
	take(B,0,n);
	int maxA = 0;
	ll maxAB = 0;
	for(int i=0;i<n;i++)
	{
		maxA = max(maxA,A[i]);
		maxAB = max(maxAB,(ll)B[i]*maxA);
		cout<<maxAB<<endl;
	}
}


/**
 * @author Macesuted (macesuted@qq.com)
 * @copyright Copyright (c) 2021
 */

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

#define int long long

#define maxn 3005
#define mod 1000000007

int a[maxn], f[maxn][maxn], cnt[maxn];

void work(void) {
    int n;
    cin >> n;
    for (register int i = 1; i <= n; i++) cin >> a[i];
    for (register int j = n; j; j--) {
        memset(cnt, 0, sizeof(cnt));
        cnt[0] = 1;
        for (register int i = n, tat = 0; i; i--) {
            tat = (tat + a[i]) % j;
            f[i][j] = cnt[tat];
            cnt[tat] = (cnt[tat] + f[i][j + 1]) % mod;
        }
    }
    cout << f[1][1] << endl;
    return;
}

signed main() {
    ios::sync_with_stdio(false);
    int _ = 1;
    // cin >> _;
    while (_--) work();
    return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define int long long
#define ld long double
using namespace __gnu_pbds;
using namespace std;

using ll = long long;

template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
	cerr << h; if (sizeof...(t)) cerr << ", ";
	DBG(t...); }
#ifndef ONLINE_JUDGE
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif

const int N = 3e3 + 5;
const int mod = 1e9 + 7;
const int INF = 1e18;
const double eps = 1e-5;
const double PI = acos(-1);

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

struct mi {
 	int v; explicit operator int() const { return v; }
	mi() { v = 0; }
	mi(ll _v):v(_v%mod) { v += (v<0)*mod; }
};
mi& operator+=(mi& a, mi b) {
	if ((a.v += b.v) >= mod) a.v -= mod;
	return a; }
mi& operator-=(mi& a, mi b) {
	if ((a.v -= b.v) < 0) a.v += mod;
	return a; }
mi operator+(mi a, mi b) { return a += b; }
mi operator-(mi a, mi b) { return a -= b; }
mi operator*(mi a, mi b) { return mi((ll)a.v*b.v); }
mi& operator*=(mi& a, mi b) { return a = a*b; }
mi pow(mi a, ll p) { assert(p >= 0); // asserts are important!
	return p==0?1:pow(a*a,p/2)*(p&1?a:1); }
mi inv(mi a) { assert(a.v != 0); return pow(a,mod-2); }
mi operator/(mi a, mi b) { return a*inv(b); }

mi dp[N][N];
int prv[N][N];
int s[N], ind[N];

void solve(int tc){
	int n;
	cin >> n;
	vector<mi> a(n+1), pref(n+1);
	for(int i=1; i<=n; i++) cin >> a[i].v;

	memset(dp, 0, sizeof dp);

	dp[0][0] = 1;

	for(int k=1; k<=n; k++){
		for(int i=1; i<=n; i++){
			s[i] = (s[i-1] + a[i].v)%k;
		}
		for(int i=0; i<k; i++){
			ind[i] = -1;
		}
		for(int i=0; i<=n; i++){
			prv[k][i] = ind[s[i]];
			ind[s[i]] = i;
		}
	}

	for(int i=1; i<=n; i++){
		for(int j=1; j<=n; j++){
			if(prv[j][i] != -1){
				dp[i][j] = dp[prv[j][i]][j-1] + dp[prv[j][i]][j];
			}
			// cout << prv[i][j] << ' ';
		}
		// cout << endl;
	}

	mi ans = 0;
	for(int j=1; j<=n; j++)
		ans += dp[n][j];
	cout << ans.v << '\n';
}

int32_t main(){
	fast;
	int tc = 1;
	// cin >> tc;
	for(int tt=1; tt<=tc; tt++){
		solve(tt);
	}
	return 0;
}


// C - Bowls and Dishes
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i=0; i < n; i++)  // 0 ~ n-1
template<class T> inline bool chmax(T& a, T& b) { if (a < b) { int tmp=a; a=b; b=tmp; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T& b) { if (a > b) { int tmp=a; a=b; b=tmp; return 1; } return 0; }
const ll mod=1000000000+7;

int main(){
    int n,m;
    cin>>n>>m;
    vector<int>a(m),b(m);
    rep(i,m){
        cin>>a[i]>>b[i];
    }
    int k;
    cin>>k;
    vector<int>c(k),d(k);
    rep(i,k){
        cin>>c[i]>>d[i];
    }

    int ans=0;
    for(int bit=0;bit<(1<<k);bit++){ // 0:put d, 1:put c
        vector<int>dishes(n);
        int cnt=0;
        for(int i=0;i<k;i++){ // k people
            if(bit&(1<<i)){
                dishes[c[i]-1]++;
            }else{
                dishes[d[i]-1]++;
            }
        }

        for(int i=0;i<m;i++){
            if(dishes[a[i]-1]>=1&&dishes[b[i]-1]>=1){
                cnt++;
            }
        }
        ans=max(ans,cnt);
    }
    cout<<ans<<endl;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define pi acos(-1.0)
#define endl "\n"
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
typedef long long ll;
typedef unsigned long long ull;

bool sortbysecc(const pair<int,int> &a, const pair<int,int> &b){
    return (a.second < b.second);
}

ll plates[200], ans;

void solve(ll a[], ll b[], ll c[], ll d[], ll i, ll n, ll k){
    if(i==k){
        ll cnt=0;
        for(ll j=0;j<n;j++){
            if(plates[a[j]]&&plates[b[j]]){
                cnt++;
            }
        }
        ans=max(cnt, ans);
        return;
    }
    plates[c[i]]++;
    solve(a, b, c, d, i+1, n, k);
    plates[c[i]]--;
    plates[d[i]]++;
    solve(a ,b, c, d, i+1, n, k);
    plates[d[i]]--;
}

int main()
{
    fastio;
    ll tc, n, m, k, i, j, f, caseno=1;
    cin>>n>>m;
    ll a[m], b[m];
    for(i=0;i<m;i++){
        cin>>a[i]>>b[i];
    }
    cin>>k;
    ll c[k], d[k];
    for(i=0;i<k;i++){
        cin>>c[i]>>d[i];
    }
    ans=0;
    solve(a, b, c, d, 0, m, k);
    cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;

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

int n;
int dp[18][(1<<18)];
int cost[18][18];
int solve(int i,int mask)
{
   if(mask==0)
   return cost[i][0];
   if(dp[i][mask]!=-1)
   return dp[i][mask];
   int ans=INT_MAX;
   for(int j=1;j<n;j++)
   {
       if(mask&(1<<j))
       {
           ans=min(ans,cost[i][j]+solve(j,mask^(1<<j)));
       }
   }
   return dp[i][mask]=ans;
}
int32_t main()
{
    fast;
    cin>>n;
    memset(dp,-1,sizeof(dp));
    vector<vector<int>>v(n,vector<int>(3));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<3;j++)
        {
            cin>>v[i][j];
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            cost[i][j]=abs(v[j][0]-v[i][0])+abs(v[j][1]-v[i][1])+max(0,v[j][2]-v[i][2]);
            cost[j][i]=abs(v[i][0]-v[j][0])+abs(v[i][1]-v[j][1])+max(0,v[i][2]-v[j][2]);
        }
    }
    for(int i=0;i<n;i++)
    {
        cost[i][i]=0;
    }
    cout<<solve(0,(((1<<n)-1)^1))<<endl;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// --------------------------------------------------------
template<class T> bool chmax(T& a, const T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T& a, const T b) { if (b < a) { a = b; return 1; } return 0; }
#define FOR(i,l,r) for (int i = (l); i < (r); ++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define MIN(c) *min_element(ALL(c))
#define MAX(c) *max_element(ALL(c))
#define SUM(c) accumulate(ALL(c), 0)
#define SUMLL(c) accumulate(ALL(c), 0LL)
#define SZ(c) ((int)(c).size())
#define debug(x) cerr << #x << " = " << (x) << '\n';
using P = pair<int,int>;
using VP = vector<P>;
using VVP = vector<VP>;
using VS = vector<string>;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<ll>;
using VVLL = vector<VLL>;
using VB = vector<bool>;
using VVB = vector<VB>;
using VD = vector<double>;
using VVD = vector<VD>;
static const double EPS = 1e-10;
static const double PI  = acos(-1.0);
static const ll MOD = 1000000007;
// static const ll MOD = 998244353;
static const int INF = 1 << 30;  // 1073741824
// static const ll INF = 1LL << 62;  // 4611686018427387904
// --------------------------------------------------------
// #include <atcoder/all>
// using namespace atcoder;

static const int MAX_N = 17;
VVI dp((1 << MAX_N), VI(MAX_N, INF));

struct city {
    int X, Y, Z;
};

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

    int N; cin >> N;

    vector<city> C(N);
    REP(i, N) {
        int X, Y, Z; cin >> X >> Y >> Z;
        C[i] = city{X, Y, Z};
    }

    VVI G(N, VI(N, INF));
    REP(i, N-1) FOR(j,i+1,N) {
        city a = C[i];
        city b = C[j];
        G[i][j] = abs(b.X - a.X) + abs(b.Y - a.Y) + max(0, b.Z - a.Z);
        G[j][i] = abs(a.X - b.X) + abs(a.Y - b.Y) + max(0, a.Z - b.Z);
    }

    dp[0][0] = 0;
    FOR(S, 1, (1 << N)) {
        FOR(u, 0, N) {
            if (!(S & (1 << u))) continue;
            int T = S - (1 << u);
            FOR(v, 0, N) {
                if (G[v][u] == INF) continue;
                dp[S][u] = min(dp[S][u], dp[T][v] + G[v][u]);
            }
        }
    }
    int ans = dp[(1 << N) - 1][0];
    cout << ans << '\n';

    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;

// a^n (mod.MOD)を求める。計算量はO(logn)
ll modpow(ll a, ll n, ll MOD = mod) {
    if(n == 0) {
        return 1;
    }
    if(n%2 == 1) {
        return (a * modpow(a, n-1, MOD)) % MOD;
    }
    return (modpow(a, n/2, MOD) * modpow(a, n/2, MOD)) % MOD;
}

// nCrをO(r)で求める。
ll Comb(ll n, ll r) {
    if(n < r) {
        return 0;
    }
    if(r > n-r) {
        return Comb(n, n-r);
    }
    ll Mul = 1, Div = 1;
    rep(i, r) {
        Mul *= (n-i);  Div *= (i+1);
        Mul %= mod;  Div %= mod;
    }
    return Mul * modpow(Div, mod - 2) % mod;
}

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

    ll s = 0;
    rep(i, n) {
        ll a;
        cin >> a;
        s += a;
    }

    out(Comb(n+m, n+s));
    re0;
}

/*
Written By mafailure
*/
#define ill
//In the name of God
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace std;
using namespace __gnu_pbds;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"
#ifdef ill
#define int long long
#endif
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename T, size_t size> ostream& operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }

#ifdef AATIF_DEBUG
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
//#define int long long
// int dx[]={-1,1,0,0}; int dy[]={0,0,1,-1};
// int dx[]={2,2,-2,-2,1,1,-1,-1}; int dy[]={1,-1,1,-1,2,-2,2,-2};
const long long mod = 1e9 + 7;
const double eps=1e-9;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> ii;
typedef vector< pair< int, int > > vii;
typedef map<int, int> mii;
typedef pair<int, ii> pip;
typedef pair<ii, int> ppi;
#define arrinp(arr,init,final,size,type) type* arr=new type[size];for(int i=init;i<final;i++)cin>>arr[i];
#define cr2d(arr,n,m,t) t**arr=new t*[n];for(int i=0;i<n;i++)arr[i]=new t[m];
#define w(t) int t;cin>>t; while(t--)
#define takeInp(n) int n;cin>>n;
#define fr(i,init,final) for(int i=init;i<final;i++)
#define frr(i,init,final) for(int i=init;i>=final;i--)
#define Fr(i,final) for(int i=0;i<final;i++)
#define Frr(i,first) for(int i=first;i>=0;i--)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define debug(x) cerr<<">value ("<<#x<<") : "<<x<<endl;
#define setb __builtin_popcount
#define lsone(n) (n&(-n))
#define rlsone(n) (n&(n-1))
#define clr(a,b) memset(a,b,sizeof(a))
const int inf= INT_MAX;
//struct point_i{int x,y;};
struct point_i{int x,y;
point_i(){ x=0; y=0;}
point_i(int x_,int y_){x=(x_);y=(y_) ;}
};

struct point
{
	float x,y;
	point (){x=y=0.0;}
	point (float x_,float y_){x=(x_); y=(y_);}
	bool operator < (point other) const{
	  if(fabs(x-other.x)>eps)return x<other.x;
	  return y<other.y;

		}
	bool operator == (point other) const {
		return fabs(other.x-x)<=eps&& fabs(other.y-y)<=eps;
		}

} ;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>  ordered_set;
vi init(string s)
{
	istringstream sin(s);
	int n;
	vi arr;
	while(sin>>n)arr.push_back(n);
	return arr;
}
int power(int x, int y)
{
	if(y==0)return 1;
	int u=power(x,y/2);
	u=(u*u)%mod;
	if(y%2)u=(x*u)%mod;
	return u;

}
int gcd(int a,int b)
{
	if(a<b)return gcd(b,a);
	return (b==0?a:(a%b?gcd(b,a%b):b));
}
int gcd_e(int a,int b,int &x,int &y)
{

	if(b==0){x=1; y=0; return a;}
	int x1,y1;
	int p=gcd_e(b,a%b,x1,y1);
	x=y1;
	y=x1-(a/b)*y1;
	return p;
}

int mul(int a,int b)
{
	return (a*b)%mod;
}
int inv(int n)
{
	return power(n,mod-2);
}

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

	vi a(n);
	fr(i,0,n) cin>>a[i];
	int sum=accumulate(all(a),n);
	int num=1;
	int den=1;
	m+=n;
	fr(i,0,sum)
	{
		num=mul(num,m-i);
		den=mul(den,sum-i);
	}
	cout<<mul(num,inv(den))<<endl;
	//fr(i,0,n) sum+=a[i];

}


#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<map>
#include<tuple>
#include<algorithm>
#include<cmath>
#include<limits>
#include<set>
#include<deque>
#include<queue>
#include<stack>
using namespace std;
#define int long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define dup(x,y) (((x)+(y)-1)/(y))
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
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; }
typedef pair<int, int>P;
const int MOD = 1e9 + 7;
//const int MOD = 998244353;
const int INF = 1e18;
const long double PI = (acos(-1));

signed main() {
	string S;
	cin >> S;
	if (S.size() == 1) {
		if (S[0] == '8')cout << "Yes";
		else cout << "No";
		return 0;
	}
	if (S.size() <= 2) {
		int a = '1' + S[0] - 1;
		int b = '1' + S[1] - 1;
		int c = 10 * a + b;
		int d = 10 * b + a;
		if (c % 8 == 0 || d % 8 == 0) {
			cout << "Yes";
		}
		else cout << "No";
		return 0;
	}
	map<int, int>m;
	for (auto i : S)m[i]++;
	for (int i = 1; i <= 9; i++) {
		for (int j = 1; j <= 9; j++) {
			for (int k = 1; k <= 9; k++) {
				char ci = '1' + i - 1;
				char cj = '1' + j - 1;
				char ck = '1' + k - 1;
				m[ci]--; m[cj]--; m[ck]--;
				if (m[ci] < 0 || m[cj] < 0 || m[ck] < 0) {
					m[ci]++; m[cj]++ + m[ck]++;
					continue;
				}
				int num = i * 100 + j * 10 + k;
				if (num % 8 == 0) {
					cout << "Yes";
					return 0;
				}
				m[ci]++; m[cj]++ + m[ck]++;
			}
		}
	}
	cout << "No";
}

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

int naive(int N, string S, string T)
{
    map<string, int> M;
    M[S] = 0;

    for (int d=0; d<100; d++)
    {
        for (auto it: M)
        {
            string s = it.first;
            int n = it.second;
            if (n==d)
            {
                for (int i=1; i<N; i++)
                    if (s[i]=='1')
                    {
                        string s2 = s;
                        s2[i] = '0';
                        s2[i-1] = s2[i-1]^1;
                        if (M.count(s2)==0)
                            M[s2] = n+1;
                    }
            }
        }
    }

    if (M.count(T)==0)
        return -1;
    return M[T];
}

long long solve(int N, string S, string T)
{
    long long ans = 0;
    int p1 = 0;
    for (int i=0; i<N; i++)
    {
        if (S[i]=='0' && T[i]=='1')
        {
            p1 = max(p1, i+1);
            while (p1<N && S[p1]=='0')
                p1++;
            if (p1>=N)
                return -1;

            S[p1] = '0';
            S[i] = '1';
            ans += p1-i;
        }
        else if (S[i]=='1' && T[i]=='0')
        {
            p1 = max(p1, i+1);
            while (p1<N && S[p1]=='0')
                p1++;
            if (p1>=N)
                return -1;

            S[i] = '0';
            S[p1] = '0';
            ans += p1-i;
        }
    }
    return ans;
}

int main()
{
    //while (true)
    //{
    //    int N = 8;
    //    string S;
    //    for (int i=0; i<N; i++)
    //        S += "01"[rand()%2];
    //    string T;
    //    for (int i=0; i<N; i++)
    //        T += "01"[rand()%2];

    //    int a1 = solve(N, S, T);
    //    int a2 = naive(N, S, T);
    //    //cout<<a1<<" "<<a2<<endl;
    //    if (a1!=a2)
    //    {
    //        cout<<N<<endl;
    //        cout<<S<<endl;
    //        cout<<T<<endl;
    //        cout<<a1<<" "<<a2<<endl;
    //        return 0;
    //    }
    //}

    int N;
    cin>>N;
    string S;
    cin>>S;
    string T;
    cin>>T;

    cout<<solve(N, S, T)<<endl;
}


#pragma region header
#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
#define int ll
#define each(i, a) for (auto &&i : (a))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using vvs = vector<vs>;
using vd = vector<ld>;
using vvd = vector<vd>;
using vb = vector<bool>;
using vvb = vector<vb>;
using P = pair<int, int>;
using vp = vector<P>;
using int128 = __int128_t;//cin coutはできない
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a / gcd(a, b) * b;};
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

template <class T>
void CVEC(const T &v) {
	int c = v.size() - 1;
	for (int i = 0; i < c; i++) cout << v[i] << ' ';
	if (c > -1) cout << v[c];
	cout << '\n';
}

#pragma endregion header

vvi es;
vi a, b;
vi depth, dp;

void DepthDFS(int v, int d){
	depth[v] = d;
	for(auto nv : es[v]){
		if(depth[nv] == -1) DepthDFS(nv, d+1);
	}
}

void ImosDFS(int v, int now){
	now += dp[v];
	dp[v] = now;
	for(auto nv : es[v]){
		if(depth[nv] > depth[v]){
			ImosDFS(nv, now);
		}
	}
}

signed main(){
    int n; cin >> n;
	es.resize(n);
	a.resize(n-1);
	b.resize(n-1);
	depth.assign(n, -1);
	dp.assign(n,0);
	rep(i,n-1){
		cin >> a[i] >> b[i];
		a[i]--, b[i]--;
		es[a[i]].push_back(b[i]);
		es[b[i]].push_back(a[i]);
	}
	DepthDFS(0,0);
	int q;
	cin >> q;
	rep(i,q){
		int t, e, x; cin >> t >> e >> x;
		e--;
		int va, vb;
		if(t == 1){
			va = a[e];
			vb = b[e];
		}else{
			va = b[e];
			vb = a[e];
		}
		if(depth[va] < depth[vb]){
			//根に足す
			dp[0] += x;
			//通らない方を引く
			dp[vb] -= x;
		}else{
			//部分木に足す
			dp[va] += x;
		}
	}
	ImosDFS(0,0);
	rep(i,n){
		cout << dp[i] << endl;
	}
}

#include<bits/stdc++.h>
#define F(i,l,r) for(register int i=l;i<=r;i++)
#define D(i,r,l) for(register int i=r;i>=l;i--)
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define p_b push_back
#define m_p make_pair
#define il inline
#define fi first
#define se second
const int INF=0x7f7f7f7f,N=2e5+5;
using namespace std;
template<typename T> void rd(T &num) {
	char ch=getchar();
	num=0;
	int k=1;
	while((ch>'9'||ch<'0')&&ch!='-') ch=getchar();
	if(ch=='-') k=-1,ch=getchar();
	while(ch<='9'&&ch>='0') num=num*10+ch-'0',ch=getchar();
	num*=k;
}
int n,T;
int u[N],v[N];
int op,e;ll x;
vector<int> mp[N];
int L[N],R[N],dep[N];
int cnt;
void dfs(int p,int fa) {
	dep[p]=dep[fa]+1;
	L[p]=++cnt;
	F(i,0,(int)mp[p].size()-1) {
		int to=mp[p][i];
		if(to!=fa) dfs(to,p);
	}
	R[p]=cnt;
}
ll ans[N<<2],tag[N<<2];
il int ls(int p) {return p<<1;}
il int rs(int p) {return p<<1|1;}
il void push_up(int p) {ans[p]=ans[ls(p)]+ans[rs(p)];}
il void f(int p,int l,int r,ll k) {
	ans[p]+=(r-l+1)*k;
	tag[p]+=k;
}
il void push_down(int p,int l,int r) {
	if(tag[p]) {
		int mid=(l+r)>>1;
		f(ls(p),l,mid,tag[p]);
		f(rs(p),mid+1,r,tag[p]);
		tag[p]=0;
	}
}
il void upd(int p,int l,int r,int nl,int nr,ll k) {
	if(nl<=l&&nr>=r) {
		f(p,l,r,k);
		return ;
	}
	push_down(p,l,r);
	int mid=(l+r)>>1;
	if(nl<=mid) upd(ls(p),l,mid,nl,nr,k);
	if(nr>mid) upd(rs(p),mid+1,r,nl,nr,k);
	push_up(p);
}
il ll query(int p,int l,int r,int ql,int qr) {
	ll res=0;
	if(ql<=l&&qr>=r) return ans[p];
	push_down(p,l,r);
	int mid=(l+r)>>1;
	if(ql<=mid) res+=query(ls(p),l,mid,ql,qr);
	if(qr>mid) res+=query(rs(p),mid+1,r,ql,qr);
	return res;
}
int main() {
	rd(n);
	F(i,1,n-1) {
		rd(u[i]),rd(v[i]);
		mp[u[i]].p_b(v[i]),mp[v[i]].p_b(u[i]);
	}
	dfs(1,0);
	// F(i,1,n) {
		// printf("dep:%d st_l:%d st_r:%d\n",dep[i],L[i],R[i]);
	// }
	rd(T);
	while(T--) {
		rd(op),rd(e),rd(x);
		int st=u[e],nd=v[e];
		if(op==2) swap(st,nd);
		if(dep[st]>dep[nd]) upd(1,1,n,L[st],R[st],x);
		else upd(1,1,n,L[1],R[1],x),upd(1,1,n,L[nd],R[nd],-x);
	}
	F(i,1,n) printf("%lld\n",query(1,1,n,L[i],L[i]));
	return 0;
}


//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include <algorithm>
#include <array>
#include <cassert>
//#include <chrono>
#include <cmath>
//#include <cstring>
//#include <functional>
//#include <iomanip>
#include <iostream>
#include <map>
//#include <numeric>
//#include <queue>
//#include <random>
#include <set>
#include <vector>
using namespace std;
#define FAST  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
#define int long long
#define ll int
#define all(a) a.begin(),a.end()
#define rev(a) a.rbegin(),a.rend()
//typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //less_equal for multiset
#define ar array
#define pb push_back
#define fi(a,b) for(int i=a;i<(b);i++)
#define fj(a,b) for(int j=a;j<(b);j++)
#define fk(a,b) for(int k=a;k<(b);k++)
const double pi=acosl(-1);

void solve()
{
    int n,k;
    cin>>n>>k;
    vector<pair<int,int>> a(n);
    vector<int> c(n);
    fi(0,n)
    {
        cin>>a[i].first>>a[i].second>>c[i];
    }
    vector<pair<int,int>> temp;
    fi(0,n)
    {
        temp.pb({a[i].first,(i+1)});
        temp.pb({a[i].second+1,-(i+1)});
    }
    sort(all(temp));
    int m=temp.size();
    // int i=0;
    // int count=0;
    int ans=0;
    multiset<int> s;
    int index=abs(temp[0].second)-1;
    int cost=c[index];
    s.insert(index);
    int i=1;
    // for(auto x:temp)
    // {
    //     cout<<x.first<<' ';
    // }
    // cout<<'\n';
    // for(auto x:temp)
    // {
    //     cout<<x.second<<' ';
    // }
    // cout<<'\n';
    while(i<m)
    {
        // cout<<i<<' '<<cost<<'\n';
        index=abs(temp[i].second)-1;
        if(temp[i].second<0)
        {
            int diff=temp[i].first-temp[i-1].first;
            ans+=diff*min(k,cost);
            int here=temp[i].first;
            int prev=i;
            while(i<n&&temp[i].first==here&&temp[i].second<0)
            {
                cost-=c[abs(temp[i].second)-1];
                i++;
            }
            if(i==prev)
            {
                cost-=c[abs(temp[i].second)-1];
                i++;
            }
        }
        else
        {
            int diff=temp[i].first-temp[i-1].first;
            ans+=diff*min(k,cost);
            cost+=c[index];
            i++;
        }
    }
    cout<<ans<<'\n';

}

signed main()
{
    FAST;
    int tt=1;
    //cin>>tt;
    while(tt--)
    {
        solve();
    }
}

//int dx[] = {+1,-1,+0,+0,-1,-1,+1,+1}; // Eight Directions
//int dy[] = {+0,+0,+1,-1,+1,-1,-1,+1}; // Eight Directions
//int dx[]= {-2,-2,-1,1,-1,1,2,2}; // Knight moves
//int dy[]= {1,-1,-2,-2,2,2,-1,1}; // Knight moves
// For (a^b)%mod, where b is large, replace b by b%(mod-1)..{because we solve this by introducing log ..do same for general cases}
// a+b = (a|b)+(a&b)
// a+b = 2*(a&b)+(a^b)

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

int main(){
	int64_t N, X;
	cin >> N >> X;
	string S;
	cin >> S;

	int64_t ans = X;
	for(int i=0;i<N;i++){
		if(S.at(i)=='x' && ans>0){
			ans--;
		}else if(S.at(i) == 'o'){
			ans++;
		}
	}

	cout << ans << endl;

	return 0;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long ll;

double xs[200000+10];
double ys[200000+10];

bool g[105][105];
int visited[105];
int counter;

int main(void) {
  int n;
  scanf("%d", &n);
  REP(i, n) {
    scanf("%lf%lf", &xs[i], &ys[i]);
  }
  int start = n;
  int goal = n+1;
  double left = 0;
  double right = 100;
  REP(iTry, 100) {
    double mid = (left + right) / 2;
    REP(i, n) {
      g[i][i] = true;
      g[i][start] = g[start][i] = (ys[i] + 100) <= 2*mid;
      g[i][goal] = g[goal][i] = (100 - ys[i]) <= 2*mid;
      REP(j, i) {
        g[i][j] = g[j][i] = sqrt((xs[i]-xs[j])*(xs[i]-xs[j]) + (ys[i]-ys[j])*(ys[i]-ys[j])) <= 2*mid;
      }
    }
    ++counter;
    queue<int> q;
    q.push(start);
    visited[start] = counter;
    while(!q.empty()){
      int cur = q.front(); q.pop();
      REP(i, n+2) {
        if(g[cur][i] && visited[i] != counter) {
          q.push(i);
          visited[i] = counter;
        }
      }
    }
    if(visited[goal] == counter) {
      right = mid;
    } else {
      left = mid;
    }
  }
  printf("%.10f\n", left);

  return 0;
}

/****************************************** By Lord Sanskar Bhargava******************************/

#include<bits/stdc++.h>
#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

#define fastio ios::sync_with_stdio(0);cin.tie(0);
#define mp make_pair
#define pb push_back
#define ep emplace_back
#define all(v) (v).begin(),(v).end()
#define dll(v) (v).rbegin(),(v).rend()
#define set1(x) __builtin_popcount(x)
#define gcd(a,b) __gcd(a,b)
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define bk(i,a,b) for(ll i=a;i>=b;i--)
#define ff first
#define ss second
#define sz(x) ((ll) (x).size())
#define mid(l,r) (l+(r-l)/2)
#define inf 1e18
const int mod = 1000000007;

template <typename T> T gcd(T a, T b){if(a%b) return gcd(b,a%b);return b;}
template <typename T> T lcm(T a, T b){return (a*(b/gcd(a,b)));}
template<typename T_vector>
void output_vector(const T_vector &v, bool add_one = false, int start = -1, int end = -1) {
    if (start < 0) start = 0;
    if (end < 0) end = int(v.size());

    for (int i = start; i < end; i++)
        cout << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\n');
}

unsigned mod_pow(unsigned a, unsigned b, unsigned mod)
{
    unsigned result = 1;

    while (b > 0) {
        if (b & 1)
            result = unsigned(uint64_t(result) * a % mod);

        a = unsigned(uint64_t(a) * a % mod);
        b >>= 1;
    }

    return result;
}
ll read(){
    ll i;
    cin>>i;
    return i;
}
vl readvl(ll n,ll off=0){
    vl v(n);
    rep(i,0,n)v[i]=read()+off;
    return v;
}
string readString(){
    string s;
    cin>>s;
    return s;
}

template<class T>
T sq(const T& t){
    return t*t;
}
void run_test()
{
    double n;
    double k=1.08;
    ll c=206;
    cin>>n;
    if((ll)(n*k)<c){
        cout<<"Yay!"<<endl;return;
    }
    if((ll)(n*k)==c){
        cout<<"so-so"<<endl;return;
    }
    if((ll)(n*k)>c){
        cout<<":("<<endl;return;
    }
}
int main(int argc, char const *argv[])
{
    clock_t begin = clock();
    fastio;
    int tt=1;
    // cin>>tt;
    while(tt--)
    {
        run_test();
    }
    #ifndef ONLINE_JUDGE
      clock_t end = clock();
      cout<<"\n\nExecuted In: "<<double(end - begin) / CLOCKS_PER_SEC*1000<<" ms";
    #endif
return 0;
}


#include<bits/stdc++.h>
#define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define Freed freopen ("0in.txt","r",stdin);
#define Fout freopen ("0out.txt","w",stdout);
#define ll long long int
#define pb push_back
#define mp make_pair
#define pi acos(-1.0)
#define Mod 1000000007
#define limit 200008
using namespace std;
ll mn[limit],mx[limit];
vector<ll> g[limit];

void dfs(ll u)
{
    ll cost = min(mn[u],mx[u]);
    for(auto v:g[u])
    {
        if(mn[v]>cost)
        {
            mn[v] = cost;  //cout <<u<<" "<<v<<' '<<mn[v]<<" "<<mx[v]<<endl;
            dfs(v);
        }
    }
}

void solve(ll tc)
{
    ll i,j,n,m,k;
    cin >> n >> m;
    for(i=0; i<limit; i++)
    {
        mn[i] = 1e18;
    }

    for(i=0; i<n; i++) cin >> mx[i];

    for(i=0; i<m; i++)
    {
        ll u, v;
        cin >> u >> v;
        u--; v--;
        g[u].pb(v);
    }
    for(i=0; i<n; i++)
    {
        dfs(i);
    }
    ll ans = -1e18;
    for(i=0; i<n; i++)
    {
        ans = max(ans,mx[i]-mn[i]);   //cout <<i<<" "<<mn[i]<<' '<<mx[i]<<" "<<ans<<endl;
    }
    cout <<ans<<endl;
    return ;
}
int  main()
{
//    Fast
//    Freed
//    Fout
    ll tc,tt=1;
   // cin >> tt;
    for(tc=1; tc<=tt; tc++)
        solve(tc);
    return 0;
}

// Author :: <Hitesh_Saini>
#include<bits/stdc++.h>

#define __speed() ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define dbg(x) cout << "[" << __LINE__ << ": "<< #x << " = " << x << " ]\n";
#define tt int t; for (cin>>t; t--; )

#define f0(i, n)  for (i = 0; i < (int)(n); i++)
#define f1(i, n)  for (i = 1; i<= (int)(n); i++)

#define cmax(x, y) x = max(x, y);
#define cmin(x, y) x = min(x, y);

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)(x.size())

#define EB emplace_back
#define PB push_back

#define endl "\n"
#define S second
#define F first

using namespace std;
using mii = map<int, int>;
using pii = pair<int, int>;

using ll = long long;
using vi = vector<int>;

using vvi = vector<vi>;
using vl = vector<ll>;

const int mod = 1e9+7, mxN = 2e5+5;

template<typename... T> void print(T... args) { ((cout << args << " "), ...); cout << endl; }

template<typename T1, typename T2> istream& operator>>(istream& in, pair<T1,T2> &a) { in>>a.F>>a.S; return in; }
template<typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1,T2> a) { out<<a.F<<" "<<a.S; return out; }

template<typename T> istream& operator>>(istream& in, vector<T> &a) { for (auto&x:a)in>>x; return in; }
template<typename T> ostream& operator<<(ostream& out, vector<T> a) { for (auto&x:a)out << x << ' '; return out; }

vi C(mxN, INT_MAX);
vi adj[mxN];

void solve() {
	int n, m, i, u, v;
	cin >> n >> m;
	vi A(n);
	cin >> A;
	vector<pii> B(n);
	f0(i, n)
		B[i].F = A[i], B[i].S = i;
	sort(all(B));
	f0(i, m) {
		cin >> u >> v;
		--u, --v;
		adj[u].EB(v);
	}

	vi vis(n);
	f0(i, n) {
		queue<int> q;
		q.push(B[i].S);
		while(sz(q)) {
			int cr = q.front();
			vis[cr] = 1;
			q.pop();
			if (cr^B[i].S)
				cmin(C[cr], A[B[i].S]);
			for (int x: adj[cr])
				if (!vis[x])
					q.push(x);
		}
		vis[B[i].S] = 0;
	}

	int ans = INT_MIN;
	f0(i, n)
		if(C[i]!=INT_MAX)
			cmax(ans, A[i]-C[i]);
	// print(C[0], C[1], C[2]);
	print(ans);
}

signed main() {
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif

    __speed() solve();
    return 0;
}


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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define REP(NAME, NUM) for (int NAME = 0; NAME < int(NUM); ++NAME)
#define BREP(NAME, NUM) for (int NAME = (NUM)-1; NAME >= 0; --NAME)
#define ALL(NAME) (NAME).begin(), (NAME).end()

#define cMOD 1000000007ULL
#define cINF ((1ll<<62)-1)
#define cINFINT ((1<<30)-1)

const ld pi = acosl(-1.0);

template <typename T>
void chmin(T& a, T b){ if(a > b) a = b; }
template <typename T>
void chmax(T& a, T b){ if(a < b) a = b; }

ll n,m;

ll in(ll y)
{
    return -m-2 <= y-n && y-n <= m+2;
}

ll c(ll y)
{
    return y - n + m+2;
}

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

	cin>>n>>m;
	vector<pair<ll,ll>> p( m );
    REP(i,m) cin>>p[i].first>>p[i].second;
    sort(ALL(p));
    map<ll, vector<ll>> xy;

    REP(i,m)
    {
        if(xy.count(p[i].first)<=0) xy[p[i].first] = vector<ll>(0);
        xy[p[i].first].emplace_back(p[i].second);
    }

    vector<bool> dp(2*m+5,false);
    map<ll, bool> update;
    dp[c(n)] = true;
    for( auto i = xy.begin(); i != xy.end() ; ++i )
    {
        //cout << i->first << ": "; REP( i, dp.size() ) cout << dp[i] << " "; cout << endl;
        //ll x = i->first;
        vector<ll>& ylist = i->second;

        update.clear();
        REP(j,ylist.size())
        {
            ll y = ylist[j];
            for(ll d=0;d<=1;d=((d==0)?-1:d+2))
            {
                if(!(in(y)&&in(y+d))) continue;
                if(d==0)
                    update[y] = false;
                else if(dp[c(y+d)])
                    update[y] = true;
            }
        }

        for( auto j = update.begin(); j != update.end() ; ++j )
        {
            //cout << i->first << ": " << j->first << " " << j->second << endl;
            dp[c(j->first)] = j->second;
        }
    }

    ll ans = 0;
    REP(i,dp.size())
        if(dp[i] == true) ++ans;
    cout << ans << endl;

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

#include <bits/stdc++.h>
#define fast ios::sync_with_stdio(0), cin.tie(nullptr)
#define all(v) v.begin(), v.end()
#define compress(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define fi first
#define se second
#define cat(a, b) a##b

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll S = 200000 + 10, MOD = (ll)1e9 + 7;
pii arr[S];
bool chk[S * 2];
int main() {
	fast;
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < m; i++)
		cin >> arr[i].fi >> arr[i].se;
	sort(arr, arr + m);
	chk[m + 1] = 1;
	vector<vector<pii>> v{ {arr[0]} };
	for (int i = 1; i < m; i++) {
		if (arr[i - 1].fi != arr[i].fi)
			v.push_back({});
		v.back().push_back(arr[i]);
	}
	queue<int> pos, impos;
	for (auto i : v) {
		for (pii j : i) {
			if (abs(j.se - n) > m or j.fi == 0)
				continue;
			int idx = j.se - n + m + 1;
			if (chk[idx - 1] or chk[idx + 1]) {
				if (!chk[idx])
					pos.push(idx);
			}
			else {
				if (chk[idx])
					impos.push(idx);
			}
		}
		while (!pos.empty()) {
			chk[pos.front()] = true;
			pos.pop();
		}
		while (!impos.empty()) {
			chk[impos.front()] = false;
			impos.pop();
		}

	}
	int ans = 0;
	for (int i = 1; i <= 2 * m + 1; i++)
		ans += chk[i];
	cout << ans;
	return 0;
}


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

void check(bool& Akaried, vector<vector<int>>& Map, int i, int j) {
	switch (Map[i][j]) {
	case 1:
		break;
	case -1:
		if (Akaried) Map[i][j] = 1;
		break;
	case 2:
		Akaried = true;
		break;
	case 3:
		Akaried = false;
		break;
	}
	return;
}

int main() {
	int H, W, N, M;
	cin >> H >> W >> N >> M;
	vector<vector<int>> Map(H, vector<int>(W, -1));
	for (int i = 0; i < N; i++) {
		int a, b;
		cin >> a >> b;
		a--, b--;
		Map[a][b] = 2;
	}
	for (int i = 0; i < M; i++) {
		int c, d;
		cin >> c >> d;
		c--, d--;
		Map[c][d] = 3;
	}
	for (int i = 0; i < H; i++) {
		bool Akaried = false;
		for (int j = 0; j < W; j++) {
			check(Akaried, Map, i, j);
		}
		for (int j = W - 1; j >= 0; j--) {
			check(Akaried, Map, i, j);
		}
	}
	for (int j = 0; j < W; j++) {
		bool Akaried = false;
		for (int i = 0; i < H; i++) {
			check(Akaried, Map, i, j);
		}
		for (int i = H - 1; i >= 0; i--) {
			check(Akaried, Map, i, j);
		}
	}
	int ans = 0;
	for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) ans += (Map[i][j] == 1 || Map[i][j] == 2);
	cout << ans << endl;
}

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

int main()
{
	int H, W, N, M;
	cin >> H >> W >> N >> M;
	vector<vector<int>> dp(H + 2, vector<int>(W + 2));
	rep(i, N)
	{
		int a, b;
		cin >> a >> b;
		dp[a][b] = 2;
	}
	rep(i, M)
	{
		int a, b;
		cin >> a >> b;
		dp[a][b] = -1;
	}

	bool judge = false;
	for (int h = 1; h <= H; h++)
	{
		judge = false;
		for (int w = 1; w <= W; w++)
		{
			if (dp[h][w] < 0){
				judge = false;
			}
			else if (dp[h][w] > 1)
			{
				judge = true;
			}
			else if (judge)
			{
				dp[h][w] = 1;
			}
		}
		judge = false;
		for (int w = W; w > 0; w--)
		{
			if (dp[h][w] < 0){
				judge = false;
			}
			else if (dp[h][w] > 1)
			{
				judge = true;
			}
			else if (judge)
			{
				dp[h][w] = 1;
			}
		}
	}

	for (int w = 1; w <= W; w++)
	{
		judge = false;
		for(int h = 1; h <= H; h++){
			if (dp[h][w] < 0){
				judge = false;
			}
			else if (dp[h][w] > 1)
			{
				judge = true;
			}
			else if (judge)
			{
				dp[h][w] = 1;
			}
		}
		judge = false;
		for(int h = H;h > 0; h--){
			if (dp[h][w] < 0){
				judge = false;
			}
			else if (dp[h][w] > 1)
			{
				judge = true;
			}
			else if (judge)
			{
				dp[h][w] = 1;
			}
		}
	}

	int ans = 0;
	for (int h = 1; h <= H; h++)
	{
		for (int w = 1; w <= W; w++)
		{
			if (dp[h][w] > 0)
				ans++;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define mk make_pair
#define pb push_back
#define INF (ll)1e18
#define pii pair<ll,ll>
#define mod 998244353
#define ff first
#define ss second
#define srt(v)  if(!v.empty())sort(v.begin(),v.end())
#define rev(v)  if(!v.empty())reverse(v.begin(),v.end())
#define PI 3.141592653589793238
#define pqr priority_queue<ll,vector<ll>,greater<ll>()>
using namespace std;
ll pow_mod(ll a,ll b)
{
	ll res=1;
	while(b!=0)
	{
		if(b&1)
		{
			res=(res*a)%mod;
		}
		a=(a*a)%mod;
		b/=2;
	}
	return res;
}

ll inv_mod(ll x){
    return pow_mod(x,mod-2);
}

const ll N=(ll)3e5;
ll inv[N];
ll fac[N];

ll ncr(ll n,ll r){
    if(n<r)
    return 0;

    if(n==r||r==0)
    return 1LL;

    ll x=fac[n];
    ll y=inv[n-r];
    ll z=inv[r];

    return ((x*y)%mod*z)%mod;
}
void solve()
{
   ll n;
   cin>>n;
   ll a[n];

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

   ll ans[n];

   ll sum[n];

   ll mx[n]={0};

   for(ll i=0;i<n;i++){
   	if(i==0)
   	sum[i]=a[i];
   	else
   	sum[i]=sum[i-1]+a[i];

   	if(i!=0)
   	mx[i]=mx[i-1];

   	mx[i]=max(mx[i],a[i]);
   }

   ll res=0;

   for(ll i=0;i<n;i++){
   	res+=sum[i];
   	ans[i]=res+mx[i]*(i+1);
   }

   for(ll i=0;i<n;i++)
   cout<<ans[i]<<"\n";

   cout<<endl;
}
int main()
{    ios_base::sync_with_stdio(false);
     cin.tie(NULL);
     cout.tie(NULL);

//Start from Here.

  ll t;
  t=1;
 // cin>>t;
  while(t--)
  solve();

  fac[0]=1;

  for(ll i=1;i<N;i++)
  fac[i]=(i*fac[i-1])%mod;

  for(ll i=0;i<N;i++)
  inv[i]=inv_mod(fac[i]);
//Good Bye!
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define int ll
const int N = (int) 2e5 + 7;
int n, a[N], b[N], aib[N];
map<int, vector<int>> pos;
map<int, int> cur;

void add(int i, int x) {
  while (i <= n) {
    aib[i] += x;
    i += i & (-i);
  }
}

int get(int i) {
  int sol = 0;
  while (i) {
    sol += aib[i];
    i -= i & (-i);
  }
  return sol;
}

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

  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
  vector<int> foo, bar;
  for (int i = 1; i <= n; i++) {
    a[i] += i;
    b[i] += i;
    foo.push_back(a[i]);
    bar.push_back(b[i]);
  }
  sort(foo.begin(), foo.end());
  sort(bar.begin(), bar.end());
  if (foo != bar) {
    cout << "-1\n";
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    pos[b[i]].push_back(i);
  }
  int sol = 0;
  for (int i = 1; i <= n; i++) {
    int j = pos[a[i]][cur[a[i]]++];
    assert(j + get(j) >= 1);
    sol += j + get(j) - 1;
    add(j, -1);
  }
  cout << sol << "\n";
  return 0;
}


// Problem: D - Base n
// Contest: AtCoder - AtCoder Beginner Contest 192
// URL: https://atcoder.jp/contests/abc192/tasks/abc192_d
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

// Disclaimer: Don't copy my template, it may lead to plagiarism.
/* Author: Soumy Jain
   Handle: soumy_jain || soumyjain
   "Beautiful flowers too, eventually wither and fall. That's the fate of all living beings."
   "I hate perfection. To be perfect is to be unable to improve any furthur." - Mayuri Kurotsuchi | Bleach
   "I smile to show the pressure of heroes and to trick the fear inside of me."
   "Gravel may be gravel, but me? I'm the gravel that shatters diamonds."
   "If you were to write a story with me in the lead role, it would certainly be...a TRAGEDY." - Kaneki Ken | Tokyo
   Ghoul
*/
/******************************************************************************/
// clang-format off
#include <bits/stdc++.h>
#define ll        long long
#define ull       unsigned long long
#define SPEEDHACK ios_base::sync_with_stdio(false);cin.tie(NULL);
#define Tc        ll tc; cin>>tc; while(tc--)
#define Qu        ll qu; cin>>qu; while(qu--)
#define ff        first
#define ss        second
#define sz(v)     (ll)(v).size()
#define file      freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define MOD       1000000007      // 998244353
using namespace std;
/******************************************************************************/
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(ll x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(ull x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template<typename T, typename V>
void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; }
template<typename T>
void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; }
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); }
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
// clang-format on

/***********************************MAIN***************************************/
// Are you ready to face the wrath of test cases? Good luck noob Soumy!

int main()
{
    SPEEDHACK
    // file
    string s;
    cin >> s;
    ll m;
    cin >> m;
    ll d = 0;
    for (ll i = 0; i < sz(s); i++)
    {
        d = max(d, (ll)s[i] - 48);
    }
    if (sz(s) == 1)
    {
        if (s[0] - 48 > m)
            cout << 0;
        else
            cout << 1;
        return 0;
    }
    ll c = 0;
    if (sz(s) == 2 || sz(s) == 3 || sz(s) == 4)
    {
        for (ll i = pow(m / (s.front() * 1.0 - 48), 1.0 / (sz(s) - 1)); i > d; i--)
        {
            ll k = 0, b = 1;
            for (ll j = sz(s) - 1; j >= 0; j--, b *= i)
            {
                k += (s[j] - 48) * b;
            }
            if (k <= m)
            {
                c = i;
                break;
            }
        }
        cout << c - d;
        return 0;
    }
    for (ll i = d + 1; i <= pow(m, 1.0 / (sz(s) - 1)); i++)
    {
        ll k = 0, b = 1;
        for (ll j = sz(s) - 1; j >= 0; j--, b *= i)
        {
            k += (s[j] - 48) * b;
        }
        if (k > m)
            break;
        c++;
    }
    cout << c;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl '\n'
//#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define BIT(n) (1LL << (n))
#define TOUPPER(s) transform(s.begin(), s.end(), s.begin(), ::toupper)
#define TOLOWER(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
#define FILLZERO(s, n)                      \
    do {                                    \
        ostringstream os;                   \
        os << setw(n) << setfill('0') << s; \
        s = os.str();                       \
    } while (0);
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())  // sortしてからつかうこと

using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pi = pair<int, int>;
template <typename T>
using Pq = priority_queue<T>;
template <typename T>
using pQ = priority_queue<T, vector<T>, greater<T>>;

template <typename T>
vector<T> make_vec(size_t a, T n) {
    return vector<T>(a, n);
}
template <typename T, typename... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
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;
}
template <typename T>
bool isProductOverflow(T a, T b) {
    return (a > numeric_limits<T>::max() / b);
}
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &x : vec) is >> x;
    return is;
}

__attribute__((constructor)) void ioInit() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    cout << std::fixed << std::setprecision(10);
}

constexpr ll MOD = 1e9 + 7;
// constexpr ll MOD = 998244353;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

long long intpow(long long a, long long n) {
    if (n == 0)
        return 1;
    else if (n & 1)
        return intpow(a * a, n / 2) * a;
    else
        return intpow(a * a, n / 2);
}

string f(ll x, ll n) {
    string res;
    while (x > 0) {
        if (x == 1 and n == 1) {
            break;
        }
        res.push_back(char(min(10LL, x % n) + '0'));
        x /= n;
    }
    reverse(ALL(res));
    return res;
}

bool comp(string &x, string &y) {
    if (x.size() > y.size()) {
        return 1;
    } else if (x.size() < y.size()) {
        return 0;
    } else {
        return x >= y;
    }
}

signed main() {
    cin >> S >> M;
    dump(S);
    string tmp = S;
    sort(RALL(tmp));
    ll n = tmp[0] - '0';

    ll ok = n + 1;
    ll ng = M + 10;
    tmp = f(M, ok);
    if (!comp(tmp, S)) {
        cout << 0 << endl;
        return 0;
    }
    tmp = f(M, ng);
    if (comp(tmp, S)) {
        if (S.size() == 1) {
            cout << 1 << endl;
            return 0;
        }
        cout << M - n << endl;
        return 0;
    }
    while (abs(ok - ng) > 1) {
        ll mid = (ok + ng) / 2;
        auto T = f(M, mid);
        if (comp(T, S)) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    cout << ok - n << endl;
}


#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std;
typedef int int_;
#define int long long
#define maxn 400010
const int inf=1e9+7;
queue<int> q;
inline int read(){
	int ret=0,ff=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') ff=-ff;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		ret=ret*10+ch-'0';
		ch=getchar();
	}
	return ret*ff;
}
struct Edge{
	int u,v,w,next;
}E[maxn<<1];
int ecnt=0,head[maxn];
int in[maxn];
void addedge(int u,int v){
	++in[v];
	E[++ecnt].u=u;
	E[ecnt].v=v;
	E[ecnt].next=head[u];
	head[u]=ecnt;
}
void Addedge(int u,int v){
	addedge(u,v);
	addedge(v,u);
}
int gcd(int a,int b){
	return b?gcd(b,a%b):a;
}
int ksm(int a,int b,int p){
	int ret=1;
	while(b){
		if(b&1) ret=ret*a%p;
		a=a*a%p;
		b>>=1;
	}
	return ret%p;
}
bool cmp(int x,int y){
	return x>y;
}
int a[10010];
int G2(int x){
	int cnt=0;
	while(x){
		a[++cnt]=x%10;
		x/=10;
	}
	sort(a+1,a+1+cnt);
	int ret=0;
	for(int i=1;i<=cnt;++i) ret=ret*10+a[i];
	return ret;
}
int G1(int x){
	int cnt=0;
	while(x){
		a[++cnt]=x%10;
		x/=10;
	}
	sort(a+1,a+1+cnt,cmp);
	int ret=0;
	for(int i=1;i<=cnt;++i) ret=ret*10+a[i];
	return ret;
}
int F(int x){
	return G1(x)-G2(x);
}
char X[10010];
bool ck(int Mid,int len,int m){
	int sum=0,tmp=1;
	for(int j = len; j;--j){
		sum+=tmp*(X[j]-'0');
		tmp*=Mid;
	}
	return sum>m;
}
int dp[2][110][110][110];
int_ main(){
	int n=read(),x=read();
	for(int i=1;i<=n;++i) a[i]=read();
	memset(dp, -1, sizeof dp);
	for(int i=1;i<=n;i++) dp[0][i][0][0] = 0;
	for(int i=0;i<n;i++){
		int qwq=i&1;
		for(int j =1;j<=n;j++){
			for(int qaq =0;qaq<=i;qaq++){
				for(int res=0;res<j;res++){
					if(dp[qwq][j][qaq][res]==-1) continue;
					dp[qwq^1][j][qaq+1][(res+a[i+1])%j]=max(dp[qwq^1][j][qaq+1][(res+a[i+1])%j],dp[qwq][j][qaq][res]+a[i+1]);
					dp[qwq^1][j][qaq][res]=max(dp[qwq^1][j][qaq][res], dp[qwq][j][qaq][res]);
				}
			}
		}
	}
	int qwq=n&1,ans=x;
	for(int qaq=1;qaq<=n;qaq++){
		if(dp[qwq][qaq][qaq][x%qaq]>0&&x>=dp[qwq][qaq][qaq][x%qaq])ans=min(ans,(x-dp[qwq][qaq][qaq][x%qaq])/qaq);
	}
	printf("%lld",ans);
	return 0;
}
/*
1
6 3
*/
/*
111111111111111111111111111111111111111111111111111111111111
1000000000000000000

100000000000000000000000000000000000000000000000000000000000

1000000000000000000
2
1000000000000000000
*/

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
inline int my_getchar(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void rd(long long &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar(a);
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar('-');
  }
  while(s--){
    my_putchar(f[s]+'0');
  }
}
template<class S, class T> inline S chmin(S &a, T b){
  if(a>b){
    a=b;
  }
  return a;
}
template<class S, class T> inline S chmax(S &a, T b){
  if(a<b){
    a=b;
  }
  return a;
}
int N;
long long X;
long long A[110];
long long f[110][110];
int main(){
  int k;
  rd(N);
  rd(X);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);
    }
  }
  long long ans = 4611686016279904256LL;
  for(k=(1);k<(N+1);k++){
    int i, j;
    for(j=(0);j<(k+1);j++){
      int l;
      for(l=(0);l<(k+1);l++){
        f[j][l] = -4611686016279904256LL;
      }
    }
    f[0][0] = 0;
    for(i=(0);i<(N);i++){
      for(j=(k)-1;j>=(0);j--){
        int l;
        for(l=(0);l<(k);l++){
          if(f[j][l] < 0){
            continue;
          }
          chmax(f[j+1][(l + A[i])%k], f[j][l] + A[i]);
        }
      }
    }
    if(f[k][X%k] < 0){
      continue;
    }
    chmin(ans, (X-f[k][X%k])/k);
  }
  wt_L(ans);
  wt_L('\n');
  return 0;
}
// cLay version 20210103-1

// --- original code ---
// //no-unlocked
// int N;
// ll X, A[110], f[110][110];
// {
//     rd(N,X, A(N));
//     ll ans = ll_inf;
//     rep(k,1,N+1){
//         rep(j, k+1)
//             rep(l, k+1)
//                 f[j][l] = -ll_inf;
//         f[0][0] = 0;
//         rep(i, N)
//             rrep(j, k)
//                 rep(l, k)
//                 {
//                     if(f[j][l] < 0) continue;
//                     f[j+1][(l + A[i])%k] >?= f[j][l] + A[i];
//                 }
//         if(f[k][X%k] < 0)
//             continue;
//         ans <?= (X-f[k][X%k])/k;
//     }
//     wt(ans);
// }