using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace AtCoder.C
{
    public class Program
    {
        public static long N { get; private set; }
        public static long M { get; private set; }
        public static long Q { get; private set; }

        public static void Main() { var r = GetResult(); Debug.WriteLine(r); Console.Write(r); }

        private static object GetResult()
        {
            (N, M, Q) = Read3Longs();
            var a = new long[Q];
            var b = new long[Q];
            var c = new long[Q];
            var d = new long[Q];
            for (int i = 0; i < Q; i++)
            {
                (a[i], b[i], c[i], d[i]) = Read4Longs();
            }

            var point = 0L;
            for (int i1 = 1; i1 <= M; i1++)
            {
                for (int i2 = i1; i2 <= M; i2++)
                {
                    if (N == 2)
                    {
                        var A = new long[] { i1, i2 };
                        point = Math.Max(point, GetPoint(a, b, c, d, A));
                        continue;
                    }
                    for (int i3 = i2; i3 <= M; i3++)
                    {
                        if (N == 3)
                        {
                            var A = new long[] { i1, i2, i3 };
                            point = Math.Max(point, GetPoint(a, b, c, d, A));
                            continue;
                        }
                        for (int i4 = i3; i4 <= M; i4++)
                        {
                            if (N == 4)
                            {
                                var A = new long[] { i1, i2, i3, i4 };
                                Debug.WriteLine(string.Join(" ", A));
                                point = Math.Max(point, GetPoint(a, b, c, d, A));
                                continue;
                            }
                            for (int i5 = i4; i5 <= M; i5++)
                            {
                                if (N == 5)
                                {
                                    var A = new long[] { i1, i2, i3, i4, i5 };
                                    point = Math.Max(point, GetPoint(a, b, c, d, A));
                                    continue;
                                }
                                for (int i6 = i5; i6 <= M; i6++)
                                {
                                    if (N == 6)
                                    {
                                        var A = new long[] { i1, i2, i3, i4, i5, i6 };
                                        point = Math.Max(point, GetPoint(a, b, c, d, A));
                                        continue;
                                    }
                                    for (int i7 = i6; i7 <= M; i7++)
                                    {
                                        if (N == 7)
                                        {
                                            var A = new long[] { i1, i2, i3, i4, i5, i6, i7 };
                                            point = Math.Max(point, GetPoint(a, b, c, d, A));
                                            continue;
                                        }
                                        for (int i8 = i7; i8 <= M; i8++)
                                        {
                                            if (N == 8)
                                            {
                                                var A = new long[] { i1, i2, i3, i4, i5, i6, i7, i8 };
                                                point = Math.Max(point, GetPoint(a, b, c, d, A));
                                                continue;
                                            }
                                            for (int i9 = i8; i9 <= M; i9++)
                                            {
                                                if (N == 9)
                                                {
                                                    var A = new long[] { i1, i2, i3, i4, i5, i6, i7, i8, i9 };
                                                    point = Math.Max(point, GetPoint(a, b, c, d, A));
                                                    continue;
                                                }
                                                for (int i10 = i9; i10 <= M; i10++)
                                                {
                                                    var A = new long[] { i1, i2, i3, i4, i5, i6, i7, i8, i9, i10 };
                                                    point = Math.Max(point, GetPoint(a, b, c, d, A));
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return point;
        }

        private static long GetPoint(long[] a, long[] b, long[] c, long[] d, long[] A)
        {
            var point = 0L;
            for (int i = 0; i < Q; i++)
            {
                if (A[b[i] - 1] - A[a[i] - 1] == c[i])
                {
                    point += d[i];
                }
            }

            return point;
        }

        #region Console.Read

        public static string ReadText() { return Console.ReadLine(); }
        public static List<string> ReadTexts() { return Console.ReadLine().Split(' ').ToList(); }
        public static long ReadLong() { return long.Parse(Console.ReadLine()); }
        public static (long, long) Read2Longs() { var ls = ReadLongs(); return (ls[0], ls[1]); }
        public static (long, long, long) Read3Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2]); }
        public static (long, long, long, long) Read4Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2], ls[3]); }
        public static (long, long, long, long, long) Read5Longs() { var ls = ReadLongs(); return (ls[0], ls[1], ls[2], ls[3], ls[4]); }
        public static List<long> ReadLongs() { return Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToList(); }

        #endregion
    }
}


Measured memory_bytes: 37484