#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;

using i64 = int64_t;

const i64 MOD = 1e9+7;

const i64 INF = 1e18+7;

struct UnionFind{
	int n;
	vector<int> par;
	UnionFind(int n) : n(n), par(n, -1){}
	int Find(int x){return par[x] < 0 ? x : Find(par[x]);}
	bool Unite(int x, int y){
		x = Find(x), y = Find(y);
		if(x == y)
			return false;
		if(par[x] > par[y])
			swap(x, y);
		par[x] += par[y];
		par[y] = x;
		return true;
	}
};

template<typename T>
struct Segtree{
	const int n;
	const T op;
	vector<T> elm;
	function<T(T, T)> f;

	Segtree(int n, T init, function<T(T, T)> f, T op = T()) :
			n(n),
			op(op),
			f(f),
			elm(2 * n, init)
	{
	}

	void set(int x, T val){
		x += n;
		elm[x] = val;
		while(x >>= 1)
			elm[x] = f(elm[2 * x], elm[2 * x + 1]);
	}

	void update(int x, T val){
		x += n;
		elm[x] = f(val, elm[x]);
		while(x >>= 1)
			elm[x] = f(elm[2 * x], elm[2 * x + 1]);
	}

	T get(int x, int y) const{
		T val = op;
		for(x += n, y += n - 1; x <= y; x >>= 1, y >>= 1){
			if(x & 1)
				val = f(val, elm[x++]);
			if(!(y & 1))
				val = f(val, elm[y--]);
		}
		return val;
	}
};


signed main(){

	int n, d;
	cin >> n >> d;
	vector<int> v(n);
	for(auto& x : v)
		cin >> x;


	auto f = [](pair<i64,int> x, pair<i64,int> y){
		return min(x, y);
	};

	Segtree<pair<i64,int>> seg_a(n, pair<i64,int>(), f, pair<i64,int>(INF, 0));
	Segtree<pair<i64,int>> seg_b(n, pair<i64,int>(), f, pair<i64,int>(INF, 0));

	auto s = [&](int i){
		seg_a.set(i, make_pair(v[i] + i64(1) * d * i, i));
		seg_b.set(i, make_pair(v[i] + i64(1) * d * (n - i - 1), i));
	};

	auto t = [&](int i){
		seg_a.set(i, make_pair(INF, i));
		seg_b.set(i, make_pair(INF, i));
	};

	for(int i = 0; i < n; ++i)
		s(i);

	UnionFind u(n);

	i64 ans = 0;

	while(u.par[u.Find(0)] != -n){
		// 子ノードの一覧
		vector<vector<int>> ch(n);
		// 接合するノード
		vector<pair<i64, int>> nex(n, make_pair(INF, 0));

		for(int i = 0; i < n; ++i)
			ch[u.Find(i)].push_back(i);

		for(int i = 0; i < n; ++i){
			if(ch[i].empty())
				continue;
			for(auto& x : ch[i])
				t(x);

			pair<i64,int> val(INF, 0);

			for(auto& x : ch[i]){
				auto p_a = seg_a.get(x + 1, n);
				p_a.first -= i64(1) * d * x;
				p_a.first += v[x];
				val = f(val, p_a);

				auto p_b = seg_b.get(0, x);
				p_b.first -= i64(1) * d * (n - 1 - x);
				p_b.first += v[x];
				val = f(val, p_b);
			}

			for(auto& x : ch[i])
				s(x);

			nex[i] = val;
		}

		for(int i = 0; i < n; ++i){
			if(nex[i].first != INF && u.Unite(i, nex[i].second))
				ans += nex[i].first;
		}
	}
	cout << ans << endl;
}

#include<bits/stdc++.h>
#define ll long long int
#define endl '\n'
#define M 1000000007
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define f first
#define s second
#define b begin
#define e end
#define pb push_back
#define mp make_pair
#define FOR(i,a,b) for(i=a;i<b;i++)
#define RFOR(i,a,b) for(i=a;i>=b;i--)
#define all(x) x.begin(),x.end()
#define itfr(it,x) for(it=x.begin();it!=x.end();it++)
#define flash ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;

int main()
{
    flash;
    //sieve();
    int T=1,t,n,m,k,i;
    // cin>>T;
    while(T--)
    {
        cin>>n>>k;
        int a[10]={0};
        FOR(i,0,k){
          cin>>t;
          a[t]=1;
        }
        vector<int> vs;
        FOR(i,0,10)
            if(a[i]==0)
                vs.pb(i);
        // for(auto it:vs)cout<<it<<" ";cout<<endl;
        int dg=0;
        for(i=n;i>0;i/=10)dg++;
        ll ans=0;
        k = pow(10,dg-1);
        int fl=0;
        int num=n;
        // cout<<dg<<" "<<k<<endl;
        while(ans<num)
        {
            if(k==0){
                ans = ans*10 + vs[0];
                continue;
            }
            m = n/k;
            n = n%k;
            k/=10;
            if(a[m]==0&fl==0){
                ans = ans*10 + m;
                continue;
            }
            dg=0;
            if(fl==0)
            {
                for(auto it:vs)
                    if(it>=m)
                    {
                        dg=it;
                        break;
                    }
                if(dg==0)
                {
                    if(vs[0]!=0)
                        dg=vs[0];
                    else
                    {
                        if(ans==0)
                            dg=vs[1];
                        else
                            dg=vs[0];
                    }
                }
                ans = ans*10+dg;
                fl=1;
            }
            else
            {
                ans = ans*10 + vs[0];
            }
        }
        cout<<ans<<endl;
    }
}