#include <bits/stdc++.h>
const long double pi = acos(-1.0);
const double eps = 1e-9;
using namespace std;
const int N = 1e3 + 1;
int n, m;
int dp[N][N];
vector<pair<int, int>> a[N];
int rec(int l, int r) {
  if (l > r) return 0;
  if (dp[l][r] != -1) return dp[l][r];
  for (int i = l; i <= r; i++) {
    int kol = 0;
    for (auto x : a[i])
      if (l <= x.first && x.second <= r && i >= x.first && i <= x.second) kol++;
    dp[l][r] = max(dp[l][r], rec(l, i - 1) + rec(i + 1, r) + kol * kol);
  }
  return dp[l][r];
}
void solve() {
  cin >> n >> m;
  for (int i = 0; i <= m; i++)
    for (int j = 0; j <= m; j++) dp[i][j] = -1;
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    for (int j = 1; j <= k; j++) {
      int l, r;
      cin >> l >> r;
      for (int q = l; q <= r; q++) a[q].push_back({l, r});
    }
  }
  rec(1, m);
  cout << dp[1][m] << endl;
}
int main() {
  ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
  cout.setf(ios::fixed), cout.precision(1);
  int step;
  step = 1;
  for (int i = 1; i <= step; i++) {
    solve();
  }
}
