using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO.Pipes;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Security.Cryptography.X509Certificates;

namespace cswork
{
    class Program
    {
        class Scanner
        {
            string[] s;
            int i;

            char[] cs = new char[] { ' ' };

            public Scanner()
            {
                s = new string[0];
                i = 0;
            }

            public string next()
            {
                if (i < s.Length) return s[i++];
                string st = Console.ReadLine();
                while (st == "") st = Console.ReadLine();

                s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
                i = 0;
                return next();
            }

            public int nextInt()
            {
                return int.Parse(next());
            }

            public long nextLong()
            {
                return long.Parse(next());
            }

            public double nextDouble()
            {
                return double.Parse(next());
            }
        }

        public Program() { }

        static void Main(string[] args)
        {
            new Program().Omame();
        }

        Scanner cin;
        const int MAXIN = 1024;
        bool[] used = new bool[MAXIN];
        int[] perm = new int[MAXIN];

        List<int[]> permList = new List<int[]>();

        public void Permutation(int pos, int n, int length)
        {
            if (pos == n)
            {
                permList.Add((int[])perm.Clone());
            }

            for (int i = 0; i < n; i++)
            {
                if (!used[i])
                {
                    perm[pos] = i;
                    used[i] = true;
                    Permutation(pos + 1, n, length);
                    used[i] = false;
                }
            }
        }

        int N;
        int M;
        int Q;
        List<int> a = new List<int>();
        List<int> b = new List<int>();
        List<int> c = new List<int>();
        List<int> d = new List<int>();
        long ans;

        void Omame()
        {
            cin = new Scanner();
            N = cin.nextInt();
            M = cin.nextInt();
            Q = cin.nextInt();

            for (int i = 0; i < Q; i++)
            {
                a.Add(cin.nextInt() - 1);
                b.Add(cin.nextInt() - 1);
                c.Add(cin.nextInt());
                d.Add(cin.nextInt());
            }
            ans = 0;
            List<int> list = new List<int>();
            list.Add(1);
            dfs(list);
            Console.WriteLine(ans);
        }

        void dfs(List<int> A)
        {
            if (A.Count == N)
            {
                long score = 0;
                for (int i = 0; i < Q; i++)
                {
                    if (A[b[i]] - A[a[i]] == c[i])
                    {
                        score += d[i];
                    }
                }
                ans = Math.Max(ans, score);
                return;
            }

            A.Add(A[A.Count - 1]);
            while (A[A.Count - 1] <= M)
            {
                dfs(A);
                A[A.Count - 1]++;
            }
            A.RemoveAt(A.Count - 1);
        }

    }
}


Measured memory_bytes: 27776