#include <bits/stdc++.h>   using namespace std;   template <typename U, typename V>   void Min(U &a, const V &b) {     if (a > b) a = b;   }   template <typename U, typename V>   void Max(U &a, const V &b) {     if (a < b) a = b;   }   template <typename U, typename V>   void add(U &a, const V &b) {     a = (a + b) % 1000000007;   }   template <typename U>   U gcd(U a, U b) {     if (a == 0) return b;     if (b == 0) return a;     if (a >= b)       return gcd(a % b, b);     else       return gcd(a, b % a);   }   int pow(int a, int b) {     int ans = 1;     while (b) {       if (b & 1) ans = 1LL * ans * a % 1000000007;       a = 1LL * a * a % 1000000007;       b >>= 1;     }     return ans;   }   struct node {     int k, d, t, K;     bool operator<(const node &a) const { return t < a.t; }   };   vector<node> a[505][505];   int dx[] = {1, 0}, dy[] = {0, 1};   long long dp[2][505][2][505];   const long long INF = 1e18;   int main() {     int T, m, n, i, j, k, K;     scanf( %d%d%d , &n, &m, &T);     int t, d;     while (T--) {       scanf( %d%d%d%d%d , &i, &j, &d, &t, &K);       a[i][j].push_back({0, d, t, K});       a[i + d][j - d].push_back({1, d, t + 1, K});       a[i + d][j].push_back({2, d, t + 2, K});       a[i][j + d].push_back({3, d, t + 3, K});     }     int now = 0;     for (int k = 0; k < m + 1; k++)       for (int i = 0; i < max(n, m) + 1; i++)         for (int j = 0; j < 2; j++) dp[now][k][j][i] = INF;     dp[0][0][0][0] = 0;     dp[0][0][1][0] = 0;     for (int len = 0; len < n + m - 1; len++) {       for (int j = 0; j < m; j++)         for (int k = 0; k < 2; k++)           for (int l = 0; l < max(n, m) + 1; l++) dp[1 - now][j][k][l] = INF;       for (int j = 0; j < m; j++) {         i = len - j;         if (i < 0) break;         if (i >= n) continue;         sort(a[i][j].begin(), a[i][j].end());         t = i + j;         for (int k = 0; k < 2; k++) {           for (int l = 0; l < ((k == 0) ? i + 1 : j + 1); l++)             if (dp[now][j][k][l] < INF) {               long long &w = dp[now][j][k][l];               for (auto &o : a[i][j]) {                 if (o.t > t) break;                 if ((t - o.t) % 4) {                   continue;                 }                 if (o.k < 2 || o.d > l) {                   w += o.K;                   continue;                 }                 int ok = 0;                 if (o.k == 2) {                   int tt = t - o.d - o.t + 2 - k;                   if (tt >= 0 && tt % 4 == 0) {                     ok = 1;                   }                 } else if (o.k == 3 && k == 1) {                   int tt = t - o.d - o.t + 3;                   if (tt >= 0 && tt % 4 == 0) {                     ok = 1;                   }                 }                 if (!ok) w += o.K;               }               for (int r = 0; r < 2; r++) {                 int first = i + dx[r], second = j + dy[r];                 if (first < 0 || first >= n || second < 0 || second >= m)                   continue;                 int nd = r == k ? l + 1 : 1;                 Min(dp[1 - now][second][r][nd], w);               }             }         }       }       now = 1 - now;     }     long long ans = INF;     for (int i = 0; i < 2; i++)       for (int j = 0; j < max(n, m) + 1; j++) Min(ans, dp[1 - now][m - 1][i][j]);     printf( %lld n , ans);   }