Problem: Bankopolis is an incredible city in which all the n crossroads are located on a straight line and numbered from 1 to n along it. On each crossroad there is a bank office.

The crossroads are connected with m oriented bicycle lanes (the i-th lane goes from crossroad ui to crossroad vi), the difficulty of each of the lanes is known.

Oleg the bank client wants to gift happiness and joy to the bank employees. He wants to visit exactly k offices, in each of them he wants to gift presents to the employees.

The problem is that Oleg don't want to see the reaction on his gifts, so he can't use a bicycle lane which passes near the office in which he has already presented his gifts (formally, the i-th lane passes near the office on the x-th crossroad if and only if min(ui, vi) < x < max(ui, vi))). Of course, in each of the offices Oleg can present gifts exactly once. Oleg is going to use exactly k - 1 bicycle lane to move between offices. Oleg can start his path from any office and finish it in any office.

Oleg wants to choose such a path among possible ones that the total difficulty of the lanes he will use is minimum possible. Find this minimum possible total difficulty.

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 80) — the number of crossroads (and offices) and the number of offices Oleg wants to visit.

The second line contains single integer m (0 ≤ m ≤ 2000) — the number of bicycle lanes in Bankopolis.

The next m lines contain information about the lanes.

The i-th of these lines contains three integers ui, vi and ci (1 ≤ ui, vi ≤ n, 1 ≤ ci ≤ 1000), denoting the crossroads connected by the i-th road and its difficulty.

Output

In the only line print the minimum possible total difficulty of the lanes in a valid path, or -1 if there are no valid paths.

Examples

Input

7 4
4
1 6 2
6 2 2
2 4 2
2 7 1


Output

6


Input

4 3
4
2 1 2
1 3 2
3 4 2
4 1 1


Output

3

Note

In the first example Oleg visiting banks by path 1 → 6 → 2 → 4.

Path 1 → 6 → 2 → 7 with smaller difficulity is incorrect because crossroad 2 → 7 passes near already visited office on the crossroad 6.

In the second example Oleg can visit banks by path 4 → 1 → 3.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64777216")
using namespace std;
const long long maxn = 83;
const double pi = 3.141592653589793238462643383279502884;
const double eps = 1e-6;
struct node {
  int min_off, max_off, cur_off, banks;
  int dist;
  node(int minn = 0, int maxx = 0, int curr = 0, int bba = 0, int dd = 0) {
    min_off = minn;
    max_off = maxx;
    cur_off = curr;
    banks = bba;
    dist = dd;
  }
  bool operator<(const node &b) const {
    if (dist > b.dist) return true;
    if (dist < b.dist) return false;
    if (cur_off < b.cur_off) return true;
    if (cur_off > b.cur_off) return false;
    if (min_off < b.min_off) return true;
    if (min_off > b.min_off) return false;
    if (max_off < b.max_off) return true;
    if (max_off > b.max_off) return false;
    if (banks < b.banks) return true;
    if (banks > b.banks) return false;
    return false;
  }
  bool fits(int cur) {
    if (min_off != maxn - 1 && cur <= min_off) return false;
    if (max_off != maxn - 1 && cur >= max_off) return false;
    return true;
  }
  node add(int cur, int add) {
    int next_min = min_off;
    int next_max = max_off;
    if (cur > cur_off) {
      if (min_off == maxn - 1) next_min = cur_off;
      next_min = max(next_min, cur_off);
    }
    if (cur < cur_off) {
      if (max_off == maxn - 1) next_max = cur_off;
      next_max = min(next_max, cur_off);
    }
    return node(next_min, next_max, cur, banks + 1, dist + add);
  }
};
int n, k;
long long m;
priority_queue<node> q;
vector<pair<int, int> > g[maxn];
int best_dist[maxn][maxn][maxn][maxn];
int best_ans = 1e9;
int get_best(node a) {
  return best_dist[a.min_off][a.max_off][a.cur_off][a.banks];
}
void set_best(node a) {
  best_dist[a.min_off][a.max_off][a.cur_off][a.banks] = a.dist;
}
int main() {
  cin >> n >> k >> m;
  fill((int *)best_dist, (int *)best_dist + maxn * maxn * maxn * maxn, 1e9);
  for (int i = 0; i < m; ++i) {
    int x, y, c;
    cin >> x >> y >> c;
    --x;
    --y;
    g[x].push_back(pair<int, int>(y, c));
  }
  for (int i = 0; i < n; ++i) {
    node cur = node(maxn - 1, maxn - 1, i, 1, 0);
    if (cur.banks == k) {
      best_ans = min(best_ans, cur.dist);
    }
    q.push(cur);
    set_best(cur);
  }
  while (!q.empty()) {
    node cur = q.top();
    q.pop();
    for (pair<int, int> to : g[cur.cur_off]) {
      if (cur.fits(to.first)) {
        node too = cur.add(to.first, to.second);
        if (too.banks == k) {
          best_ans = min(best_ans, too.dist);
        }
        if (get_best(too) > too.dist) {
          set_best(too);
          q.push(too);
        }
      }
    }
  }
  if (best_ans == 1e9)
    cout << -1;
  else
    cout << best_ans;
}