#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int d,n;
int t[222];
vector<int> m[66];
int c[222];

int dp[222][222];
int solve(int i,int b){
  if(i == d) return 0;
  if(dp[i][b] >= 0) return dp[i][b];

  int tt = t[i];
  int ret = 0;
  //cout << i << ":" << m[tt].size() << endl;
  for(int j = 0; j < (int)m[tt].size(); j++){
    int a = m[tt][j];
    ret = max(ret,solve(i+1,a) + ((i > 0)?abs(c[a] - c[b]):0));
  }

  return dp[i][b] = ret;
}

int main(void){
  cin >> d >> n;

  for(int i = 0; i < d; i++){
    cin >> t[i];
  }

  for(int i = 0; i < n; i++){
    int a,b;
    cin >> a >> b >> c[i];
    for(int j = a; j <= b; j++){
      m[j].push_back(i);
    }
  }

  memset(dp,-1,sizeof(dp));
  cout << solve(0,0) << endl;

}