#include <bits/stdc++.h>
using namespace std;
inline int gi(){
  char ch=getchar();int x=0,q=0;
  while(ch<'0' || ch>'9') ch=='-'?q=1:0,ch=getchar();
  while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
  return q?(-x):x;
}
int f[35000][17],w[35000],c[17][17];
void dp(int &x,int y){
  if (x<y) x=y;
  return;
}
int main(){
  int n=gi(),m=gi(),z,a,b,p,S=(1<<n)-1,d=0;
  for (int i=1; i<=m; ++i){
    a=gi(),b=gi(),p=gi();
    c[a][b]+=p,c[b][a]+=p;
    z=(1<<(a-1))|(1<<(b-1));
    for (int s=1; s<(1<<n); ++s)
      if ((s&z)==z)
	w[s]+=p;
    d+=p;
  }
  memset(f,-100,sizeof(f));
  f[1][1]=0;
  for (int s=1; s<=S; ++s)
    for (int i=1; i<=n; ++i)
      if (s&(1<<(i-1))){
	for (int j=1; j<=n; ++j)
	  if (!(s&(1<<(j-1)))&&c[i][j])
	    dp(f[s|(1<<(j-1))][j],f[s][i]+c[i][j]);
	for (int t=S^s; t; t=(t-1)&(S^s))
	  dp(f[s|t][i],f[s][i]+w[t|1<<(i-1)]);
      }
  int ans=0;
  for (int s=1; s<=S; ++s)
    dp(ans,f[s][n]);
  cout<<d-ans;
  return 0;
}

// besmellah
#include <bits/stdc++.h>

using namespace std;
typedef long long int64;
typedef pair<int,int> pii;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()

const int maxn = 1e4+10;
const int64 MO = 1000000007;
const int64 IN = 1000000000;

int d;
string s;
int dp[maxn][105];

int main () {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> s >> d;
	for (int i = 0; i < maxn; i++)
		for (int t = 0; t < 105; t++)
			dp[i][t] = 0;

	dp[0][0] = 1;
	for (int i = 0; i <= 9; i++)
		dp[1][i % d]++;
	for (int i = 2; i <= sz(s); i++)
		for (int k = 0; k < d; k++)
			for (int t = 0; t <= 9; t++)
				dp[i][(t + k) % d] = (dp[i][(t + k) % d] + dp[i - 1][k]) % MO;
	
	int cur = 0;
	int ans = -1;
	for (int i = 0; i < sz(s); i++) {
		for (int t = 0; t < s[i] - '0'; t++) {
			int f = (cur + t) % d;
			f = (d - f) % d;
			ans = (ans + dp[sz(s) - i - 1][f]) % MO;
		}
		cur = (cur + s[i] - '0') % d;
	}
	if (cur == 0)
		ans++;
	ans = (ans + MO) % MO;
	cout << ans << "\n";
}
