using System;
using System.Collections.Generic;
using System.Windows;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Atcoder
{
    public class Program
    {
        static void Main(string[] args)
        {
            var r = ReadSplit();
            var n = r[0].Int();
            var m = r[1].Int();
            var q = r[2].Int();

            Dfs(new List<int>(), 1, n, m);

            var ans = 0D;
            var l = new List<int[]>();
            for (int i = 0; i < q; i++)
            {
                var r2 = ReadSplit();
                var a = r2[0].Int();
                var b = r2[1].Int();
                var c = r2[2].Int();
                var d = r2[3].Int();
                var inta = new int[4];
                inta[0] = a;
                inta[1] = b;
                inta[2] = c;
                inta[3] = d;
                l.Add(inta);
            }
            for (int i = 0; i < ll.Count; i++)
            {
                var total = 0;
                for (int j = 0; j < q; j++)
                {
                    var aa = l[j][0];
                    var bb = l[j][1];
                    var cc = l[j][2];
                    var dd = l[j][3];
                    if (ll[i][bb - 1] - ll[i][aa - 1] == cc)
                    {
                        total += dd;
                    }
                }
                if (ans <= total)
                {
                    ans = total;
                }
            }
            WriteRead(ans);
        }

        private static List<List<int>> ll = new List<List<int>>();
        private static void Dfs(List<int> l, int v, int n, int m)
        {
            var tmp = new List<int>();
            tmp.AddRange(l);
            if (tmp.Count == n)
            {
                ll.Add(tmp);
                return;
            }
            if(v != 0) tmp.Add(v);
            while (v <= m)
            {
                Dfs(tmp, v, n, m);
                v++;
            }
        }

        public static string Read()
        {
            return Console.ReadLine();
        }

        public static string[] ReadSplit()
        {
            return Console.ReadLine().Split(' ');
        }

        public static void WriteRead(object str)
        {
            Console.WriteLine(str);
            Read();
        }

        public static void Write(object str)
        {
            Console.WriteLine(str);
        }

        private static bool ChangeMin<T>(ref T a, T b) where T : IComparable
        {
            if (a.CompareTo(b) > 0)
            {
                a = b;
                return true;
            }
            return false;
        }

        private static bool ChangekMax<T>(ref T a, T b) where T : IComparable
        {
            if (b.CompareTo(a) > 0)
            {
                a = b;
                return true;
            }
            return false;
        }
    }
}


public static class Extentions
{
    public static int Int(this object obj)
    {
        return int.Parse(obj.ToString());
    }

    public static long Long(this object obj)
    {
        return long.Parse(obj.ToString());
    }

    public static decimal Decimal(this object obj)
    {
        return decimal.Parse(obj.ToString());
    }

    public static double Double(this object obj)
    {
        return double.Parse(obj.ToString());
    }

    public static T2 GetValueZero<T1, T2>(this Dictionary<T1, T2> d, T1 key) where T2 : struct
    {
        T2 value;
        if (d.TryGetValue(key, out value))
        {
            return value;
        }
        return new T2();
    }
}



Measured memory_bytes: 36496