#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i  =  a; i < (int)(b); ++i)
#define REP(i,  n) for (int i  =  0; i < (int)(n); ++i)
#define REPR(i,  n) for (int i  =  n - 1; i >= 0; --i)
using namespace std;
#define int long long int
//------------ATCODER TEMPLATE-------------

signed main() {
  int N, M; cin >> N >> M;
  vector<int> A(N);
  REP(i, N) cin >> A[i];

  vector<pair<int, int>> ps(M);
  REP(i, M) cin >> ps[i].first >> ps[i].second;
  sort(ps.begin(), ps.end());

  vector<pair<int, int>> nodes(N);
  for (auto [a, b] : ps) {
    --a, --b;
    int x = A[a];
    if (nodes[a].second > 0) {
      x = min(A[a], nodes[a].second);
    }
    if (nodes[b].second == 0) {
      nodes[b] = {A[b], x};
    } else {
      nodes[b].second = min(nodes[b].second, x);
    }
  }

  int vmax = -1e18;
  for (auto [a, b] : nodes) {
    if (a == 0 && b == 0) continue;
    vmax = max(vmax, a - b);
  }
  cout << vmax << endl;

  return 0;
}

#include <bits/stdc++.h>

// ====================================================================
// clang-format off
namespace std {
template <class Fun>
class y_combinator_result {
    Fun fun_;
public:
    template <class T> explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
    template <class ...Args> decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); }
};
template<class Fun> decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); }
} // namespace std
// clang-format on
// ====================================================================

signed main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int64_t x, y;
    std::cin >> x >> y;

    std::map<int64_t, int64_t> map;

    auto ans = std::y_combinator([&](auto self, int64_t t) -> int64_t {
        if (map.find(t) != end(map)) {
            return map[t];
        }
        else if (t == 1) {
            return map[t] = std::abs(x - t);
        }
        else if (t & 1) {
            return map[t] = std::min({
                std::abs(x - t),
                self((t + 1) / 2) + 2,
                self((t - 1) / 2) + 2
            });
        }
        else {
            return map[t] = std::min(std::abs(x - t), self(t / 2) + 1);
        }
    })(y);

    std::cout << ans << '\n';

    return 0;
}