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

int main() {
  int D, N;
  cin >> D >> N;
  vector<int> T(D), A(N), B(N), C(N);
  for (int i = 0; i < D; ++i) {
    cin >> T[i];
  }
  for (int i = 0; i < N; ++i) {
    cin >> A[i] >> B[i] >> C[i];
  }

  vector<vector<int>> dp(D + 1, vector<int>(N + 1, -1));
  for (int i = 0; i < N; ++i) {
    if (A[i] <= T[0] && T[0] <= B[i]) {
      dp[1][i + 1] = 0;
    }
  }

  for (int i = 1; i < D; ++i) {
    for (int j = 0; j < N; ++j) {
      if (dp[i][j + 1] == -1) {
        continue;
      }

      for (int k = 0; k < N; ++k) {
        if (A[k] <= T[i] && T[i] <= B[k]) {
          dp[i + 1][k + 1] =
              max(dp[i][j + 1] + abs(C[j] - C[k]), dp[i + 1][k + 1]);
        }
      }
    }
  }

  int ans = -1;
  for (int i = 1; i <= N; ++i) {
    ans = max(dp[D][i], ans);
  }

  cout << ans << endl;
}
