#include <bits/stdc++.h>
using namespace std;
long long power(long long b, long long p, long long m) {
  if (b == 0) return 0;
  long long res = 1;
  while (p) {
    if (p % 2) {
      res = (res * b) % m;
      p--;
    } else {
      b = (b * b) % m;
      p /= 2;
    }
  }
  return res;
}
long long Root(long long r, vector<long long> &root) {
  if (!r) return 0;
  while (root[r] != r) {
    root[r] = root[root[r]];
    r = root[r];
  }
  return r;
}
void EE(long long a, long long b, long long &x, long long &y) {
  long long t;
  if (b == 0) {
    y = 0, x = 1;
    return;
  }
  EE(b, a % b, x, y);
  t = x;
  x = y;
  y = t - a / b * y;
}
void build(long long node, long long start, long long end, vector<long long> &a,
           vector<long long> &tree) {
  if (start == end) {
    tree[node] = a[start];
    return;
  }
  long long mid = (start + end) / 2;
  build(2 * node, start, mid, a, tree);
  build(2 * node + 1, mid + 1, end, a, tree);
  tree[node] = min(tree[2 * node], tree[2 * node + 1]);
}
pair<long long, long long> query(long long node, long long start, long long end,
                                 long long l, long long r,
                                 vector<pair<long long, long long> > &tree) {
  if (start >= l && end <= r) {
    return tree[node];
  }
  if (l > end || r < start) return make_pair(LONG_LONG_MAX, 0);
  long long mid = (start + end) / 2;
  pair<long long, long long> p1, p2;
  p1 = query(2 * node, start, mid, l, r, tree);
  p2 = query(2 * node + 1, mid + 1, end, l, r, tree);
  return make_pair(min(p1.first, p2.first), max(p1.second, p2.second));
}
void update(long long node, long long start, long long end, long long idx,
            long long val, vector<pair<long long, long long> > &tree) {
  if (start == end) {
    tree[node] = make_pair(val, val);
    return;
  }
  long long mid = (start + end) / 2;
  if (idx >= start && idx <= mid)
    update(2 * node, start, mid, idx, val, tree);
  else
    update(2 * node + 1, mid + 1, end, idx, val, tree);
  tree[node].first = min(tree[2 * node].first, tree[2 * node + 1].first);
  tree[node].second = max(tree[2 * node].second, tree[2 * node + 1].second);
}
vector<vector<long long> > mm(vector<vector<long long> > &a,
                              vector<vector<long long> > &b) {
  long long r, c1, c2, i, j, k;
  r = a.size();
  c1 = b.size();
  c2 = b[0].size();
  vector<vector<long long> > res(r, vector<long long>(c2));
  for (j = 0; j < r; j++) {
    for (k = 0; k < c2; k++) {
      for (i = 0; i < c1; i++) {
        res[j][k] = (res[j][k] + a[j][i] * b[i][k]) % 1000000007;
      }
    }
  }
  return res;
}
vector<vector<long long> > exp(vector<vector<long long> > &M, long long power) {
  long long m = M.size();
  vector<vector<long long> > id(m, vector<long long>(m));
  long long i, j;
  for (i = 0; i < m; i++) id[i][i] = 1;
  while (power > 0) {
    if (power % 2) id = mm(id, M);
    M = mm(M, M);
    power /= 2;
  }
  return id;
}
long long hashv(long long l, long long r, vector<long long> &pre, long long m,
                long long p) {
  long long t = (pre[r] - pre[l - 1] + m) % m;
  long long in = power(p, (l - 1) * (m - 2), m);
  return (t * in) % m;
}
void hashb(long long p, long long m, string &s, vector<long long> &pre) {
  long long i, t = p;
  for (i = 1; i < pre.size(); i++) {
    pre[i] = (pre[i - 1] + (s[i - 1] - 96) * t) % m;
    t = (t * p) % m;
  }
}
long long lca(long long a, long long b, vector<vector<long long> > &dp,
              long long n, vector<long long> &lev) {
  long long d = lev[b] - lev[a], j, i;
  for (i = 0; d > 0; i++, d /= 2) {
    if (d % 2) b = dp[b][i];
  }
  if (a == b) return b;
  for (j = log(n) / log(2); j >= 0; j--) {
    if (dp[a][j] != -1 && dp[a][j] != dp[b][j]) {
      a = dp[a][j];
      b = dp[b][j];
    }
  }
  return dp[a][0];
}
bool fc(long long a, long long b, vector<pair<long long, long long> > &v) {
  long long i;
  for (i = 0; i < v.size(); i++)
    if (v[i].first != a && v[i].first != b && v[i].second != a &&
        v[i].second != b)
      return false;
  return true;
}
long long n, m;
bool B(long long r, vector<long long> &v) {
  vector<long long> temp(n);
  long long i;
  if ((v[n - 1] + r) % m <= v[n - 1] && r)
    temp[n - 1] = m - 1;
  else
    temp[n - 1] = (v[n - 1] + r) % m;
  for (i = n - 2; i >= 0; i--) {
    if ((temp[i + 1] - v[i] + m) % m <= r)
      temp[i] = temp[i + 1];
    else
      temp[i] = (v[i] + r) % m;
  }
  for (i = 0; i < n - 1; i++)
    if (temp[i] > temp[i + 1]) return false;
  return true;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  srand(time(NULL));
  ;
  long long i, l, h, mid, ans;
  cin >> n >> m;
  vector<long long> v(n);
  for (i = 0; i < n; i++) cin >> v[i];
  l = 0, h = n * m;
  while (l <= h) {
    mid = (l + h) / 2;
    if (B(mid, v)) {
      ans = mid;
      h = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans;
}
