#include <bits/stdc++.h>
using namespace std;

#define int long long
#define INF 1e18+5
#define endl "\n"

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    vector< vector<char> > v(n+1, vector<char>(n+1));
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            cin >> v[i][j];
        }
    }
    vector< vector<int> > dp(n+1, vector<int>(n+1));
    for(int i = 0; i <= n; i++){
        dp[0][i] = dp[i][0] = INF;
    }
    dp[1][1] = (v[1][1] != 'a');
    priority_queue< tuple<int, char, int, int>, vector< tuple< int, char, int, int> >, greater< tuple<int, char, int, int> > > pq;
    int best = 0;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if(i == 1 && j == 1) continue;
            dp[i][j] += min(dp[i-1][j], dp[i][j-1]) + (v[i][j] != 'a');
            if(dp[i][j] == k){
                best = max(best, i+j);
                //ADD TO THE QUEUE
            }
        }
    }
    for(int i = 1; i <=n; i++){
        for(int j = 1; j <= n; j++){
            if(i+j == best && dp[i][j] == k){
                //cout << "PUSHED " << i << ' ' << j << endl;

                pq.push(make_tuple(i+j, 'a', i, j));
            }
        }
    }
    vector<int> depth(2*n+2, INF);
    depth[best] = (int)'a';
    while(!pq.empty()){
        auto p = pq.top();
        pq.pop();
        int a = get<0>(p);
        char b = get<1>(p);
        int c = get<2>(p);
        int d = get<3>(p);
        if(b != depth[a]) continue;
        if(a == 2*n){
            for(int i = 0; i < best-1; i++){
                cout << 'a';
            }
            for(int i = best+1; i <= 2*n; i++){
                cout << (char)depth[i];
            }
            cout << endl;
            return 0;
        }
        else{
            if(c + 1 <= n && depth[a+1] > (int)v[c+1][d]){
                depth[a+1] = (int)v[c+1][d];
                pq.push(make_tuple(a+1,v[c+1][d],c+1,d));
            }
            if(d + 1 <= n && depth[a+1] > (int)v[c][d+1]){
                depth[a+1] = (int)v[c][d+1];
                pq.push(make_tuple(a+1,v[c][d+1],c,d+1));
            }
        }
    }
}


#include <cstdio>
#include <string>
#include <vector>

#ifdef DBG
  #define D(y) y
#else
  #define D(y)
#endif

using namespace std;

const int MAXN = 2020, inf = 1023456789;

int N, K;
char grid[MAXN][MAXN];
int min_cost[MAXN][MAXN];

int dist(int r, int c) {
  return N - 1 + N - 1 - r - c;
}

string get_best_path(const vector<pair<int, int>>& seeds) {
  string res;
  res.push_back(grid[seeds[0].first][seeds[0].second]);

  vector<pair<int, int>> cur(seeds.begin(), seeds.end());
  while (true) {
    char min_neighbor = 'z' + 1;
    for (int i = 0; i < cur.size(); ++i) {
      if (cur[i].first < N - 1) {
        min_neighbor = min(min_neighbor, grid[cur[i].first + 1][cur[i].second]);
      }
      if (cur[i].second < N - 1) {
        min_neighbor = min(min_neighbor, grid[cur[i].first][cur[i].second + 1]);
      }
    }

    if (min_neighbor == 'z' + 1) {
      break;
    }

    res.push_back(min_neighbor);

    vector<pair<int, int>> next;
    for (int i = 0; i < cur.size(); ++i) {
      if (cur[i].first < N - 1 && grid[cur[i].first + 1][cur[i].second] == min_neighbor) {
        next.push_back({cur[i].first + 1, cur[i].second});
      }
      if (cur[i].second < N - 1 && grid[cur[i].first][cur[i].second + 1] == min_neighbor) {
        next.push_back({cur[i].first, cur[i].second + 1});
      }
    }

    cur = move(next);
  }

  return res;
}

int main() {
  scanf("%d %d", &N, &K);
  for (int i = 0; i < N; ++i) {
    scanf("%s", grid[i]);
    for (int j = 0; j < N; ++j) {
      min_cost[i][j] = inf;
    }
  }

  if (K == 0) {
    vector<pair<int, int>> v = {{0, 0}};
    string s = get_best_path(v);
    printf("%s\n", s.c_str());
    return 0;
  }

  min_cost[0][0] = (grid[0][0] == 'a' ? 0 : 1);
  int closest_dist = inf;
  vector<pair<int, int>> starting_locs;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (i == 0 && j == 0) continue;

      if (i > 0) min_cost[i][j] = min_cost[i - 1][j];
      if (j > 0) min_cost[i][j] = min(min_cost[i][j], min_cost[i][j - 1]);
      if (grid[i][j] != 'a') min_cost[i][j]++;

      if (min_cost[i][j] <= K) {
        int d = dist(i, j);
        if (d < closest_dist) {
          closest_dist = d;
          starting_locs.clear();
          starting_locs.push_back({i, j});
        } else if (d == closest_dist) {
          starting_locs.push_back({i, j});
        }
      }
    }
  }

  D(
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        printf("%d ", min_cost[i][j]);
      }
      printf("\n");
    }
  );

  string best = get_best_path(starting_locs);
  for (int i = 0; i < 2 * N - 1 - best.size() + 1; ++i) {
    printf("a");
  }
  printf("%s\n", best.substr(1).c_str());

  return 0;
}


#include <cstdio>
#include <string>
#include <vector>

#ifdef DBG
  #define D(y) y
#else
  #define D(y)
#endif

using namespace std;

const int MAXN = 2020, inf = 1023456789;

int N, K;
char grid[MAXN][MAXN];
int min_cost[MAXN][MAXN];
int vis[MAXN][MAXN];

int dist(int r, int c) {
  return N - 1 + N - 1 - r - c;
}

string get_best_path(const vector<pair<int, int>>& seeds) {
  string res;
  res.push_back(grid[seeds[0].first][seeds[0].second]);

  for (int i = 0; i < seeds.size(); ++i) {
    vis[seeds[i].first][seeds[i].second] = 1;
  }

  vector<pair<int, int>> cur(seeds.begin(), seeds.end());
  while (true) {
    char min_neighbor = 'z' + 1;
    for (int i = 0; i < cur.size(); ++i) {
      if (cur[i].first < N - 1) {
        min_neighbor = min(min_neighbor, grid[cur[i].first + 1][cur[i].second]);
      }
      if (cur[i].second < N - 1) {
        min_neighbor = min(min_neighbor, grid[cur[i].first][cur[i].second + 1]);
      }
    }

    D(printf("min neighbor is %c\n", min_neighbor));

    if (min_neighbor == 'z' + 1) {
      break;
    }

    res.push_back(min_neighbor);

    vector<pair<int, int>> next;
    for (int i = 0; i < cur.size(); ++i) {
      if (cur[i].first < N - 1 && grid[cur[i].first + 1][cur[i].second] == min_neighbor && !vis[cur[i].first + 1][cur[i].second]) {
        next.push_back({cur[i].first + 1, cur[i].second});
        vis[cur[i].first + 1][cur[i].second] = 1;
      }
      if (cur[i].second < N - 1 && grid[cur[i].first][cur[i].second + 1] == min_neighbor && !vis[cur[i].first][cur[i].second + 1]) {
        next.push_back({cur[i].first, cur[i].second + 1});
        vis[cur[i].first][cur[i].second + 1] = 1;
      }
    }

    cur = move(next);
  }

  return res;
}

int main() {
  scanf("%d %d", &N, &K);
  for (int i = 0; i < N; ++i) {
    scanf("%s", grid[i]);
    for (int j = 0; j < N; ++j) {
      min_cost[i][j] = inf;
    }
  }

  if (K == 0) {
    vector<pair<int, int>> v = {{0, 0}};
    string s = get_best_path(v);
    printf("%s\n", s.c_str());
    return 0;
  }

  min_cost[0][0] = (grid[0][0] == 'a' ? 0 : 1);
  int closest_dist = inf;
  vector<pair<int, int>> starting_locs;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (i == 0 && j == 0) continue;

      if (i > 0) min_cost[i][j] = min_cost[i - 1][j];
      if (j > 0) min_cost[i][j] = min(min_cost[i][j], min_cost[i][j - 1]);
      if (grid[i][j] != 'a') min_cost[i][j]++;

      if (min_cost[i][j] <= K) {
        int d = dist(i, j);
        if (d < closest_dist) {
          closest_dist = d;
          starting_locs.clear();
          starting_locs.push_back({i, j});
        } else if (d == closest_dist) {
          starting_locs.push_back({i, j});
        }
      }
    }
  }

  D(
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        printf("%d ", min_cost[i][j]);
      }
      printf("\n");
    }
  );

  D(printf("found %d starting locs\n", (int)starting_locs.size()));
  string best = get_best_path(starting_locs);
  for (int i = 0; i < 2 * N - 1 - best.size() + 1; ++i) {
    printf("a");
  }
  printf("%s\n", best.substr(1).c_str());

  return 0;
}


#include <cstdio>
#include <string>
#include <vector>

#ifdef DBG
  #define D(y) y
#else
  #define D(y)
#endif

using namespace std;

const int MAXN = 2020, inf = 1023456789;

int N, K;
char grid[MAXN][MAXN];
string best_path[MAXN][MAXN];
int min_cost[MAXN][MAXN];

int dist(int r, int c) {
  return N - 1 + N - 1 - r - c;
}

int main() {
  scanf("%d %d", &N, &K);
  for (int i = 0; i < N; ++i) {
    scanf("%s", grid[i]);
    for (int j = 0; j < N; ++j) {
      min_cost[i][j] = inf;
    }
  }

  for (int i = N - 1; i >= 0; --i) {
    for (int j = N - 1; j >= 0; --j) {
      string best = string(1, grid[i][j]) + "";
      D(printf("best_path[%d][%d] = %s\n", i, j, best.c_str()));
      if (i < N - 1) best = string(1, grid[i][j]) + best_path[i + 1][j];
      D(printf("best_path[%d][%d] = %s\n", i, j, best.c_str()));
      if (j < N - 1) best = best.size() == 1 ? string(1, grid[i][j]) + best_path[i][j + 1] : min(best, string(1, grid[i][j]) + best_path[i][j + 1]);
      best_path[i][j] = best;

      D(printf("best_path[%d][%d] = %s\n", i, j, best.c_str()));
    }
  }

  D(
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        printf("%s\t\t", best_path[i][j].c_str());
      }
      printf("\n");
    }
  );

  if (K == 0) {
    // special case
    string answer = best_path[0][0];
    for (int i = 0; i < answer.size(); ++i) {
      printf("%c", answer[i]);
    }
    printf("\n");
    return 0;
  }

  min_cost[0][0] = (grid[0][0] == 'a' ? 0 : 1);
  int closest_dist = inf;
  vector<pair<int, int>> starting_locs;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (i == 0 && j == 0) continue;

      if (i > 0) min_cost[i][j] = min_cost[i - 1][j];
      if (j > 0) min_cost[i][j] = min(min_cost[i][j], min_cost[i][j - 1]);
      if (grid[i][j] != 'a') min_cost[i][j]++;

      if (min_cost[i][j] <= K) {
        int d = dist(i, j);
        if (d < closest_dist) {
          closest_dist = d;
          starting_locs.clear();
          starting_locs.push_back({i, j});
        } else if (d == closest_dist) {
          starting_locs.push_back({i, j});
        }
      }
    }
  }

  D(
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        printf("%d ", min_cost[i][j]);
      }
      printf("\n");
    }
  );

  string best;
  for (int i = 0; i < starting_locs.size(); ++i) {
    string test = best_path[starting_locs[i].first][starting_locs[i].second];
    D(
      printf("%d %d %s\n", starting_locs[i].first, starting_locs[i].second, test.c_str());
    );
    if (best.size() == 0 || test < best) {
      best = test;
    }
  }

  for (int i = 0; i < 2 * N - 1 - (best.size() - 1); ++i) {
    printf("a");
  }
  for (int i = 1; i < best.size(); ++i) {
    printf("%c", best[i]);
  }
  printf("\n");

  return 0;
}


#include <cstdio>
#include <string>
#include <vector>

#ifdef DBG
  #define D(y) y
#else
  #define D(y)
#endif

using namespace std;

const int MAXN = 2020, inf = 1023456789;

int N, K;
char grid[MAXN][MAXN];
int best_dir[MAXN][MAXN]; // 0 = down, 1 = right
int min_cost[MAXN][MAXN];

int dist(int r, int c) {
  return N - 1 + N - 1 - r - c;
}

string get_best_path(int r, int c, bool skip_first = false) {
  string res;
  bool skipped = false;
  while (r < N && c < N) {
    if (skip_first && !skipped) {
      skipped = true;
    } else {
      res.push_back(grid[r][c]);
    }
    if (best_dir[r][c] == 0) r++;
    else c++;
  }
  return res;
}

void print_best_path(int r, int c, bool skip_first) {
  printf("%s\n", get_best_path(r, c, skip_first).c_str());
}

int main() {
  scanf("%d %d", &N, &K);
  for (int i = 0; i < N; ++i) {
    scanf("%s", grid[i]);
    for (int j = 0; j < N; ++j) {
      min_cost[i][j] = inf;
    }
  }

  for (int i = N - 1; i >= 0; --i) {
    for (int j = N - 1; j >= 0; --j) {
      if (i == N - 1 && j == N - 1) {
        best_dir[i][j] = 0;
      } else if (i == N - 1) {
        best_dir[i][j] = 1;
      } else if (j == N - 1) {
        best_dir[i][j] = 0;
      } else {
        if (get_best_path(i + 1, j) < get_best_path(i, j + 1)) {
          best_dir[i][j] = 0;
        } else {
          best_dir[i][j] = 1;
        }
      }
    }
  }

  if (K == 0) {
    print_best_path(0, 0, false);
    return 0;
  }

  min_cost[0][0] = (grid[0][0] == 'a' ? 0 : 1);
  int closest_dist = inf;
  vector<pair<int, int>> starting_locs;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (i == 0 && j == 0) continue;

      if (i > 0) min_cost[i][j] = min_cost[i - 1][j];
      if (j > 0) min_cost[i][j] = min(min_cost[i][j], min_cost[i][j - 1]);
      if (grid[i][j] != 'a') min_cost[i][j]++;

      if (min_cost[i][j] <= K) {
        int d = dist(i, j);
        if (d < closest_dist) {
          closest_dist = d;
          starting_locs.clear();
          starting_locs.push_back({i, j});
        } else if (d == closest_dist) {
          starting_locs.push_back({i, j});
        }
      }
    }
  }

  D(
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        printf("%d ", min_cost[i][j]);
      }
      printf("\n");
    }
  );

  string best;
  for (int i = 0; i < starting_locs.size(); ++i) {
    string test = get_best_path(starting_locs[i].first, starting_locs[i].second, true);
    D(
      printf("%d %d %s\n", starting_locs[i].first, starting_locs[i].second, test.c_str());
    );
    if (best.size() == 0 || test < best) {
      best = test;
    }
  }

  for (int i = 0; i < 2 * N - 1 - best.size(); ++i) {
    printf("a");
  }
  printf("%s\n", best.c_str());

  return 0;
}


#include <cstdio>
#include <string>
#include <vector>

#ifdef DBG
  #define D(y) y
#else
  #define D(y)
#endif

using namespace std;

const int MAXN = 2020, inf = 1023456789;

int N, K;
char grid[MAXN][MAXN];
int min_cost[MAXN][MAXN];
int vis[MAXN][MAXN];

int dist(int r, int c) {
  return N - 1 + N - 1 - r - c;
}

string get_best_path(const vector<pair<int, int>>& seeds) {
  string res;
  res.push_back(grid[seeds[0].first][seeds[0].second]);

  for (int i = 0; i < seeds.size(); ++i) {
    vis[seeds[i].first][seeds[i].second] = 1;
  }

  vector<pair<int, int>> cur(seeds.begin(), seeds.end());
  while (true) {
    char min_neighbor = 'z' + 1;
    for (int i = 0; i < cur.size(); ++i) {
      if (cur[i].first < N - 1) {
        min_neighbor = min(min_neighbor, grid[cur[i].first + 1][cur[i].second]);
      }
      if (cur[i].second < N - 1) {
        min_neighbor = min(min_neighbor, grid[cur[i].first][cur[i].second + 1]);
      }
    }

    D(printf("min neighbor is %c\n", min_neighbor));

    if (min_neighbor == 'z' + 1) {
      break;
    }

    res.push_back(min_neighbor);

    vector<pair<int, int>> next;
    for (int i = 0; i < cur.size(); ++i) {
      if (cur[i].first < N - 1 && grid[cur[i].first + 1][cur[i].second] == min_neighbor && !vis[cur[i].first + 1][cur[i].second]) {
        next.push_back({cur[i].first + 1, cur[i].second});
        vis[cur[i].first + 1][cur[i].second] = 1;
      }
      if (cur[i].second < N - 1 && grid[cur[i].first][cur[i].second + 1] == min_neighbor && !vis[cur[i].first][cur[i].second + 1]) {
        next.push_back({cur[i].first, cur[i].second + 1});
        vis[cur[i].first][cur[i].second + 1] = 1;
      }
    }

    cur = move(next);
  }

  return res;
}

int main() {
  scanf("%d %d", &N, &K);
  for (int i = 0; i < N; ++i) {
    scanf("%s", grid[i]);
    for (int j = 0; j < N; ++j) {
      min_cost[i][j] = inf;
    }
  }

  if (K == 0) {
    vector<pair<int, int>> v = {{0, 0}};
    string s = get_best_path(v);
    printf("%s\n", s.c_str());
    return 0;
  }

  min_cost[0][0] = (grid[0][0] == 'a' ? 0 : 1);
  int closest_dist = inf;
  vector<pair<int, int>> starting_locs;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (i == 0 && j == 0) min_cost[i][j] = 0;

      if (i > 0) min_cost[i][j] = min_cost[i - 1][j];
      if (j > 0) min_cost[i][j] = min(min_cost[i][j], min_cost[i][j - 1]);
      if (grid[i][j] != 'a') min_cost[i][j]++;

      if (min_cost[i][j] <= K) {
        int d = dist(i, j);
        if (d < closest_dist) {
          closest_dist = d;
          starting_locs.clear();
          starting_locs.push_back({i, j});
        } else if (d == closest_dist) {
          starting_locs.push_back({i, j});
        }
      }
    }
  }

  D(
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        printf("%d ", min_cost[i][j]);
      }
      printf("\n");
    }
  );

  D(printf("found %d starting locs\n", (int)starting_locs.size()));
  string best = get_best_path(starting_locs);
  for (int i = 0; i < 2 * N - 1 - best.size() + 1; ++i) {
    printf("a");
  }
  printf("%s\n", best.substr(1).c_str());

  return 0;
}


#include <cstdio>
#include <string>
#include <vector>

#ifdef DBG
  #define D(y) y
#else
  #define D(y)
#endif

using namespace std;

const int MAXN = 2020, inf = 1023456789;

int N, K;
char grid[MAXN][MAXN];
string best_path[MAXN][MAXN];
int best_dir[MAXN][MAXN]; // 0 = down, 1 = right
int min_cost[MAXN][MAXN];

int dist(int r, int c) {
  return N - 1 + N - 1 - r - c;
}

string get_best_path(int r, int c, bool skip_first) {
  string res;
  bool skipped = false;
  while (r < N && c < N) {
    if (skip_first && !skipped) {
      skipped = true;
    } else {
      res.push_back(grid[r][c]);
    }
    if (best_dir[r][c] == 0) r++;
    else c++;
  }
  return res;
}

void print_best_path(int r, int c, bool skip_first) {
  printf("%s\n", get_best_path(r, c, skip_first).c_str());
}

int main() {
  scanf("%d %d", &N, &K);
  for (int i = 0; i < N; ++i) {
    scanf("%s", grid[i]);
    for (int j = 0; j < N; ++j) {
      min_cost[i][j] = inf;
    }
  }

  for (int i = N - 1; i >= 0; --i) {
    for (int j = N - 1; j >= 0; --j) {
      if (i == N - 1 && j == N - 1) {
        best_path[i][j] = string(1, grid[i][j]);
      } else if (i == N - 1) {
        best_path[i][j] = string(1, grid[i][j]) + best_path[i][j + 1];
        best_dir[i][j] = 1;
      } else if (j == N - 1) {
        best_path[i][j] = string(1, grid[i][j]) + best_path[i + 1][j];
        best_dir[i][j] = 0;
      } else {
        if (best_path[i + 1][j] < best_path[i][j + 1]) {
          best_path[i][j] = string(1, grid[i][j]) + best_path[i + 1][j];
          best_dir[i][j] = 0;
        } else {
          best_path[i][j] = string(1, grid[i][j]) + best_path[i][j + 1];
          best_dir[i][j] = 1;
        }
      }
    }

    if (i + 1 < N - 1) {
      for (int j = 0; j < N; ++j) {
        best_path[i + 1][j].clear();
      }
    }
  }

  if (K == 0) {
    print_best_path(0, 0, false);
    return 0;
  }

  min_cost[0][0] = (grid[0][0] == 'a' ? 0 : 1);
  int closest_dist = inf;
  vector<pair<int, int>> starting_locs;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (i == 0 && j == 0) continue;

      if (i > 0) min_cost[i][j] = min_cost[i - 1][j];
      if (j > 0) min_cost[i][j] = min(min_cost[i][j], min_cost[i][j - 1]);
      if (grid[i][j] != 'a') min_cost[i][j]++;

      if (min_cost[i][j] <= K) {
        int d = dist(i, j);
        if (d < closest_dist) {
          closest_dist = d;
          starting_locs.clear();
          starting_locs.push_back({i, j});
        } else if (d == closest_dist) {
          starting_locs.push_back({i, j});
        }
      }
    }
  }

  D(
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        printf("%d ", min_cost[i][j]);
      }
      printf("\n");
    }
  );

  string best;
  for (int i = 0; i < starting_locs.size(); ++i) {
    string test = get_best_path(starting_locs[i].first, starting_locs[i].second, true);
    D(
      printf("%d %d %s\n", starting_locs[i].first, starting_locs[i].second, test.c_str());
    );
    if (best.size() == 0 || test < best) {
      best = test;
    }
  }

  for (int i = 0; i < 2 * N - 1 - best.size(); ++i) {
    printf("a");
  }
  printf("%s\n", best.c_str());

  return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>

const int maxn = 2e3 + 7;

class Solution{
private :
    int n, k, cnt;
    char s[maxn][maxn];
    int f[maxn][maxn];
    bool vis[maxn][maxn];

    char ans[maxn * 2];
    int tot;

    struct Node{
        int x, y;

        Node (int x, int y) :
            x(x),
            y(y) {}
    };

    int now, last;
    std :: queue<Node> q[2], tmp;

    void Make() {
        for (register int i = 1; i + cnt < n + n; i++) {
            char z = 'z' + 1;
            while (!q[last].empty()) {
                Node nd = q[last].front();
                tmp.push(nd);
                q[last].pop();
                if (nd.x < n) {
                    z = std :: min(z, s[nd.x + 1][nd.y]);
                }
                if (nd.y < n) {
                    z = std :: min(z, s[nd.x][nd.y + 1]);
                }
            }
            while (!tmp.empty()) {
                Node nd = tmp.front();
                tmp.pop();
                if (nd.x < n) {
                    if (s[nd.x + 1][nd.y] == z && !vis[nd.x + 1][nd.y]) {
                        vis[nd.x + 1][nd.y] = 1;
                        q[now].push(Node(nd.x + 1, nd.y));
                    }
                }
                if (nd.y < n) {
                    if (s[nd.x][nd.y + 1] == z && !vis[nd.x][nd.y + 1]) {
                        vis[nd.x][nd.y + 1] = 1;
                        q[now].push(Node(nd.x, nd.y + 1));
                    }
                }
            }
            std :: swap(now, last);
        }
    }

    void DFS(int x, int y) {
        if (x <= 0 || y <= 0) {
            return;
        }
        if (x + y == cnt) {
            return;
        }
        ans[++tot] = s[x][y];
        if (vis[x - 1][y]) {
            DFS(x - 1, y);
        } else {
            DFS(x, y - 1);
        }
    }

public :
    Solution() {
        now = 1;
        last = 0;
        Get();
        Solve();
    }

    void Get() {
        scanf("%d %d", &n, &k);
        for (register int i = 1; i <= n; i++) {
            scanf("%s", s[i] + 1);
        }
    }

    void Solve() {
        memset(f, 0x3f, sizeof(f));
        f[1][1] = s[1][1] == 'a' ? 0 : 1;
        for (register int i = 1; i <= n; i++) {
            for (register int j = 1; j <= n; j++) {
                if (i == 1 && j == 1) {
                    continue;
                }
                f[i][j] = std :: min(f[i - 1][j], f[i][j - 1]);
                if (s[i][j] != 'a') {
                    f[i][j]++;
                }
            }
        }
        for (register int i = 1; i <= n; i++) {
            for (register int j = 1; j <= n; j++) {
                if (f[i][j] <= k && i + j > cnt) {
                    cnt = i + j;
                }
            }
        }
        for (register int i = 1; i <= n; i++) {
            for (register int j = 1; j <= n; j++) {
                if (i + j == cnt && f[i][j] <= k) {
                    vis[i][j] = 1;
                    q[last].push(Node(i, j));
                }
            }
        }
        if (q[last].empty()) {
            vis[1][1] = 1;
            q[last].push(Node(1, 1));
        }
        Make();
        DFS(n, n);
        for (register int i = 1; i < cnt; i++) {
            putchar('a');
        }
        for (register int i = tot; i >= 1; i--) {
            putchar(ans[i]);
        }
        putchar('\n');
    }
};
Solution sol;

int main() {}


//
//  main.cpp
//  B
//
//  Created by 徐润泽 on 2020/2/19.
//  Copyright © 2020 徐润泽. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
int n, k;
char a[2005][2005];
int f[2005][2005];
bool vis[2005][2005];
struct node{
    int x, y;
};
char ans[4005];
int main(int argc, const char * argv[]) {
    cin>>n>>k;
    for (int i = 1; i <= n; i++) {
        cin>>(a[i] + 1);
    }
    f[1][1] = a[1][1] == 'a' ? 0 : 1;
    vis[1][1] = 1;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if(i == 1 && j == 1) continue;
            if(i == 1) {
                f[i][j] = f[i][j - 1];
            }
            else if (j == 1) {
                f[i][j] = f[i - 1][j];
            }
            else {
                f[i][j] = min(f[i][j - 1], f[i - 1][j]);
            }
            if(a[i][j] != 'a') f[i][j]++;
            if((f[i - 1][j] <= k && i - 1 >= 1) || (f[i][j - 1] <= k && j - 1 >= 1))
                vis[i][j] = 1;
        }
    }
    if(f[n][n] <= k){
        for (int i = 1; i <= 2 * n - 1; i++) {
            cout<<'a';
        }
        cout<<endl;
        return 0;
    }
    queue<node>q;
    char cur = 'z';
    int pos = 0;
    vector<node>t;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (f[i][j] == k + 1 && vis[i][j]) {
                if(a[i][j] == cur){
                    if(i + j - 2 > pos) pos = i + j - 2;
                    if(i + j - 2 == pos)
                        t.push_back((node){i, j});
                }
                else if(a[i][j] < cur){
                    if(i + j - 2 > pos) pos = i + j - 2;
                    if(i + j - 2 == pos) {
                        cur = a[i][j];
                        t.clear();
                        t.push_back((node){i, j});
                    }
                }
            }
        }
    }
    cur = 'z';
    for (int i = 0; i < pos; i++) {
        ans[i] = 'a';
    }
    while (1) {
        if(q.empty()){
            ans[pos] = a[t[0].x][t[0].y];
            for (int i = 0; i < t.size(); i++) {
                q.push(t[i]);
            }
            t.clear();
            if(pos == 2 * n - 2) break;
            pos++;
            cur = 'z';
        }
        node temp = q.front();
        q.pop();
        int x = temp.x, y = temp.y;
        if(x + 1 <= n){
            if(a[x + 1][y] < cur){
                cur = a[x + 1][y];
                t.clear();
                t.push_back((node){x + 1, y});
            }
            else if(a[x + 1][y] == cur){
                t.push_back((node){x + 1, y});
            }
        }
        if(y + 1 <= n){
            if(a[x][y + 1] < cur){
                cur = a[x][y + 1];
                t.clear();
                t.push_back((node){x, y + 1});
            }
            else if(a[x][y + 1] == cur){
                t.push_back((node){x, y + 1});
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}


//
//  main.cpp
//  B
//
//  Created by 徐润泽 on 2020/2/19.
//  Copyright © 2020 徐润泽. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
int n, k;
char a[2005][2005];
int f[2005][2005];
bool vis[2005][2005];
struct node{
    int x, y;
    char c;
};
char ans[4005];
int main(int argc, const char * argv[]) {
    cin>>n>>k;
    for (int i = 1; i <= n; i++) {
        cin>>(a[i] + 1);
    }
    f[1][1] = a[1][1] == 'a' ? 0 : 1;
    vis[1][1] = 1;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if(i == 1 && j == 1) continue;
            if(i == 1) {
                f[i][j] = f[i][j - 1];
            }
            else if (j == 1) {
                f[i][j] = f[i - 1][j];
            }
            else {
                f[i][j] = min(f[i][j - 1], f[i - 1][j]);
            }
            if(a[i][j] != 'a') f[i][j]++;
            if((f[i - 1][j] <= k && i - 1 >= 1) || (f[i][j - 1] <= k && j - 1 >= 1))
                vis[i][j] = 1;
        }
    }
    if(f[n][n] <= k){
        for (int i = 1; i <= 2 * n - 1; i++) {
            cout<<'a';
        }
        cout<<endl;
        return 0;
    }
    queue<node>q;
    char cur = 'z';
    int pos = 0;
    vector<node>t;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (f[i][j] == k + 1 && vis[i][j]) {
                if(a[i][j] == cur){
                    if(i + j - 2 > pos) pos = i + j - 2;
                    if(i + j - 2 == pos)
                        t.push_back((node){i, j, a[i][j]});
                }
                else if(a[i][j] < cur){
                    if(i + j - 2 > pos) pos = i + j - 2;
                    if(i + j - 2 == pos) {
                        cur = a[i][j];
                        t.clear();
                        t.push_back((node){i, j, cur});
                    }
                }
            }
        }
    }
    cur = 'z';
    for (int i = 0; i < pos; i++) {
        ans[i] = 'a';
    }
    while (1) {
        if(q.empty()){
            ans[pos] = t[0].c;
            for (int i = 0; i < t.size(); i++) {
                q.push(t[i]);
            }
            t.clear();
            if(pos == 2 * n - 2) break;
            pos++;
            cur = 'z';
        }
        node temp = q.front();
        q.pop();
        int x = temp.x, y = temp.y;
        if(x + 1 <= n){
            if(a[x + 1][y] < cur){
                cur = a[x + 1][y];
                t.clear();
                t.push_back((node){x + 1, y, cur});
            }
            else if(a[x + 1][y] == cur){
                t.push_back((node){x + 1, y, cur});
            }
        }
        if(y + 1 <= n){
            if(a[x][y + 1] < cur){
                cur = a[x][y + 1];
                t.clear();
                t.push_back((node){x, y + 1, cur});
            }
            else if(a[x][y + 1] == cur){
                t.push_back((node){x, y + 1, cur});
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int N, K;
string S[2000];
const int dx[] = {0, 1};
const int dy[] = {1, 0};
bool inside(int x, int y){
    return (x>=0 && x<N && y>=0 && y<N);
}
const int INF = 1e9;
int dist[2000][2000];
bool visited[2000][2000];

int main(){
    int i, j, k;
    cin >> N >> K;
    for(i=0; i<N; i++) cin >> S[i];

    for(i=0; i<N; i++){
        for(j=0; j<N; j++) dist[i][j] = INF;
    }
    deque<pair<int, int>> que;
    vector<pair<int, int>> cand;
    int sum = -1;
    que.push_back({0, 0});
    dist[0][0] = (S[0][0] == 'a' ? 0 : 1);
    if(dist[0][0] <= K){
        cand.push_back({i, j});
        sum = 0;
    }

    while(que.size()){
        auto p = que.front(); que.pop_front();
        int i = p.first, j = p.second;
        for(k=0; k<2; k++){
            int x = i+dx[k], y = j+dy[k];
            if(inside(x, y) && dist[x][y] == INF){
                if(S[x][y] == 'a'){
                    dist[x][y] = dist[i][j];
                    que.push_front({x, y});
                }else{
                    dist[x][y] = dist[i][j] + 1;
                    que.push_back({x, y});
                }

                if(dist[x][y] > K) continue;
                if(x+y >= sum){
                    if(x+y > sum) cand.clear();
                    sum = x+y;
                    cand.push_back({x, y});
                }
            }
        }
    }
    if(cand.size() == 0) cand = {{-1, 0}};

    string ans(sum+1, 'a');
    for(int s=sum+1; s<2*N-1; s++){
        vector<pair<int, int>> cand2;
        char mn = '|';
        for(auto p : cand){
            int i = p.first, j = p.second;
            for(k=0; k<2; k++){
                int x = i+dx[k], y = j+dy[k];
                if(!inside(x, y) || visited[x][y]) continue;
                visited[x][y] = true;
                if(S[x][y] <= mn){
                    if(S[x][y] < mn) cand2.clear();
                    mn = S[x][y];
                    cand2.push_back({x, y});
                }
            }
        }
        ans += mn;
        cand.clear();
        cand = cand2;
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int N, K;
string S[2000];
const int dx[] = {0, 1};
const int dy[] = {1, 0};
bool inside(int x, int y){
    return (x>=0 && x<N && y>=0 && y<N);
}
const int INF = 1e9;
int dist[2000][2000];

int main(){
    int i, j, k;
    cin >> N >> K;
    for(i=0; i<N; i++) cin >> S[i];

    for(i=0; i<N; i++){
        for(j=0; j<N; j++) dist[i][j] = INF;
    }
    deque<pair<int, int>> que;
    vector<pair<int, int>> cand;
    int sum = -1;
    que.push_back({0, 0});
    dist[0][0] = (S[0][0] == 'a' ? 0 : 1);
    if(dist[0][0] <= K){
        cand.push_back({i, j});
        sum = 0;
    }

    while(que.size()){
        auto p = que.front(); que.pop_front();
        int i = p.first, j = p.second;
        for(k=0; k<2; k++){
            int x = i+dx[k], y = j+dy[k];
            if(inside(x, y) && dist[x][y] == INF){
                if(S[x][y] == 'a'){
                    dist[x][y] = dist[i][j];
                    que.push_front({x, y});
                }else{
                    dist[x][y] = dist[i][j] + 1;
                    que.push_back({x, y});
                }

                if(dist[x][y] > K) continue;
                if(x+y >= sum){
                    if(x+y > sum) cand.clear();
                    sum = x+y;
                    cand.push_back({x, y});
                }
            }
        }
    }
    if(cand.size() == 0) cand = {{-1, 0}};

    string ans(sum+1, 'a');
    for(int s=sum+1; s<2*N-1; s++){
        vector<pair<int, int>> cand2;
        char mn = '|';
        for(auto p : cand){
            int i = p.first, j = p.second;
            for(k=0; k<2; k++){
                int x = i+dx[k], y = j+dy[k];
                if(!inside(x, y)) continue;
                if(S[x][y] <= mn){
                    if(S[x][y] < mn) cand2.clear();
                    mn = S[x][y];
                    cand2.push_back({x, y});
                }
            }
        }
        ans += mn;
        cand.clear();
        cand = cand2;
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int N, K;
string S[2000];
const int dx[] = {0, 1};
const int dy[] = {1, 0};
bool inside(int x, int y){
    return (x>=0 && x<N && y>=0 && y<N);
}
const int INF = 1e9;
int dist[2000][2000];

int main(){
    int i, j, k;
    cin >> N >> K;
    for(i=0; i<N; i++) cin >> S[i];

    for(i=0; i<N; i++){
        for(j=0; j<N; j++) dist[i][j] = INF;
    }
    deque<pair<int, int>> que;
    vector<pair<int, int>> cand;
    int sum = -1;
    que.push_back({0, 0});
    dist[0][0] = (S[0][0] == 'a' ? 0 : 1);
    if(dist[0][0] <= K){
        cand.push_back({i, j});
        sum = 0;
    }

    while(que.size()){
        auto p = que.front(); que.pop_front();
        int i = p.first, j = p.second;
        for(k=0; k<2; k++){
            int x = i+dx[k], y = j+dy[k];
            if(inside(x, y) && dist[x][y] == INF){
                if(S[x][y] == 'a'){
                    dist[x][y] = dist[i][j];
                    que.push_front({x, y});
                }else{
                    dist[x][y] = dist[i][j] + 1;
                    que.push_back({x, y});
                }

                if(dist[x][y] > K) continue;
                if(x+y >= sum){
                    if(x+y > sum) cand.clear();
                    sum = x+y;
                    cand.push_back({x, y});
                }
            }
        }
    }
    if(cand.size() == 0) cand = {{-1, 0}};

    string ans(sum+1, 'a');
    for(int s=sum+1; s<2*N-1; s++){
        vector<pair<int, int>> cand2;
        char mn = '|';
        for(auto p : cand){
            int i = p.first, j = p.second;
            for(k=0; k<2; k++){
                int x = i+dx[k], y = j+dy[k];
                if(!inside(x, y)) continue;
                if(S[x][y] <= mn){
                    if(S[x][y] < mn) cand2.clear();
                    mn = S[x][y];
                    cand2.push_back({x, y});
                }
            }
        }
        ans += mn;
        cand = cand2;
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int N, K;
string S[2000];
const int dx[] = {0, 1};
const int dy[] = {1, 0};
bool inside(int x, int y){
    return (x>=0 && x<N && y>=0 && y<N);
}
const int INF = 1e9;
int dist[2000][2000];
bool visited[2000][2000];

int main(){
    int i, j, k;
    cin >> N >> K;
    for(i=0; i<N; i++) cin >> S[i];

    for(i=0; i<N; i++){
        for(j=0; j<N; j++) dist[i][j] = INF;
    }
    deque<pair<int, int>> que;
    vector<pair<int, int>> cand;
    int sum = -1;
    que.push_back({0, 0});
    dist[0][0] = (S[0][0] == 'a' ? 0 : 1);
    if(dist[0][0] <= K){
        cand.push_back({0, 0});
        sum = 0;
    }

    while(que.size()){
        auto p = que.front(); que.pop_front();
        int i = p.first, j = p.second;
        for(k=0; k<2; k++){
            int x = i+dx[k], y = j+dy[k];
            if(inside(x, y) && dist[x][y] == INF){
                if(S[x][y] == 'a'){
                    dist[x][y] = dist[i][j];
                    que.push_front({x, y});
                }else{
                    dist[x][y] = dist[i][j] + 1;
                    que.push_back({x, y});
                }

                if(dist[x][y] > K) continue;
                if(x+y >= sum){
                    if(x+y > sum) cand.clear();
                    sum = x+y;
                    cand.push_back({x, y});
                }
            }
        }
    }
    if(cand.size() == 0) cand = {{-1, 0}};

    string ans(sum+1, 'a');
    for(int s=sum+1; s<2*N-1; s++){
        vector<pair<int, int>> cand2;
        char mn = '|';
        for(auto p : cand){
            int i = p.first, j = p.second;
            for(k=0; k<2; k++){
                int x = i+dx[k], y = j+dy[k];
                if(!inside(x, y) || visited[x][y]) continue;
                visited[x][y] = true;
                if(S[x][y] <= mn){
                    if(S[x][y] < mn) cand2.clear();
                    mn = S[x][y];
                    cand2.push_back({x, y});
                }
            }
        }
        ans += mn;
        cand.clear();
        cand = cand2;
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>

#define MAX 2005

typedef std::pair<int, int> pii;

char grid[MAX][MAX];
int dist[MAX][MAX];

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n,k;
    std::cin >> n >> k;

    for (int i = 0; i < n; i++) {
        std::cin >> grid[i];
    }

    dist[0][0] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            dist[i][j] = i + j;

            if (i != 0) {
                dist[i][j] = std::min(dist[i][j], dist[i-1][j]);
            }

            if (j != 0) {
                dist[i][j] = std::min(dist[i][j], dist[i][j-1]);
            }

            if (grid[i][j] != 'a')
                dist[i][j]++;
        }
    }

    int mpref = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (dist[i][j] == k)
                mpref = std::max(mpref, i + j + 1);
        }
    }

    std::vector<pii> q;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if ((i + j) == mpref && dist[i][j] <= (k + 1))
                q.push_back({i, j});
        }
    }

    std::string suf;
    while (!q.empty()) {
        char best = 'z';

        std::vector<pii> cache;
        for (pii p : q) {
            if (grid[p.first][p.second] <= best) {
                if (grid[p.first][p.second] < best) {
                    cache.clear();
                }
                best = grid[p.first][p.second];
                std::cout << best << " " << p.first << " " << p.second << "\n";
                for (int dx : {0,1}) {
                    int dy = 1 - dx;
                    int nx = p.first + dx;
                    int ny = p.second + dy;

                    if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
                    cache.push_back({nx, ny});
                }
            }
        }

        suf.push_back(best);
        q = cache;

        sort(q.begin(), q.end());
        q.erase(unique(q.begin(), q.end()), q.end());
    }

    std::string pref;
    for (int i = 0; i < mpref; i++) {
        pref.push_back('a');
    }

    std::cout << (pref+suf) << "\n";
}


#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>

#define MAX 2005
#define INF 1e9

typedef std::pair<int, int> pii;

char grid[MAX][MAX];
int dist[MAX][MAX];

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n,k;
    std::cin >> n >> k;

    for (int i = 0; i < n; i++) {
        std::cin >> grid[i];
    }

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            dist[i][j] = INF;

    int md = 0;
    dist[0][0] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i != 0) {
                dist[i][j] = std::min(dist[i][j], dist[i-1][j]);
            }

            if (j != 0) {
                dist[i][j] = std::min(dist[i][j], dist[i][j-1]);
            }

            if (grid[i][j] != 'a')
                dist[i][j]++;

            md = std::max(md, dist[i][j]);
        }
    }

    int mpref = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (dist[i][j] <= k)
                mpref = std::max(mpref, i + j + 1);
        }
    }

    std::vector<pii> q;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if ((i + j + 1) == mpref && dist[i][j] <= k)
                q.push_back({i, j});
        }
    }

    if (k == 0 && q.empty()) q.push_back({0,-1});

    std::string suf;
    while (!q.empty()) {
        char best = 'z';
        std::vector<pii> cache;
        for (pii p : q) {
            for (int dx : {0,1}) {
                int dy = 1 - dx;
                int nx = p.first + dx;
                int ny = p.second + dy;

                if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
                if (grid[nx][ny] > best) continue;

                if (grid[nx][ny] < best) {
                    cache.clear();
                }

                best = grid[nx][ny];
                cache.push_back({nx, ny});
            }
        }

        if (cache.empty()) break;

        suf.push_back(best);
        q = cache;

        sort(q.begin(), q.end());
        q.erase(unique(q.begin(), q.end()), q.end());
    }

    std::string pref;
    for (int i = 0; i < mpref; i++) {
        pref.push_back('a');
    }

    std::cout << (pref+suf) << "\n";
}


#include <iostream>
#include <string>

#define MAX 2005

char grid[MAX][MAX];
int vis[MAX][MAX] = {{0}};
int dr[] = {0, 1};
int dc[] = {1, 0};

std::string dfs(int x, int y, int n) {
    std::string res;
    vis[x][y] = 1;
    res.push_back(grid[x][y]);

    std::string temp = "";
    for (int i = 0; i < 2; i++) {
        int nx = x + dr[i];
        int ny = y + dc[i];

        if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
        if (vis[nx][ny]) continue;

        std::string run = dfs(nx, ny, n);
        if (temp == "" || run < temp) {
            temp = run;
        }
    }

    vis[x][y] = 0;
    return res + temp;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n,k;
    std::cin >> n >> k;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            std::cin >> grid[i][j];
        }
    }

    std::string res = dfs(0, 0, n);

    for (int i = 0; i < res.size() && k > 0; i++) {
        if (res[i] == 'a') continue;
        res[i] = 'a';
        k--;
    }

    std::cout << res << "\n";
}


#include <iostream>
#include <string>
#include <cstring>

#define MAX 2005

char grid[MAX][MAX];
int dr[] = {0, 1};
int dc[] = {1, 0};
std::string memo[MAX][MAX];

std::string dfs(int x, int y, int px, int py, int n) {
    std::string res;
    res.push_back(grid[x][y]);

    if (memo[x][y] != "") return memo[x][y];

    std::string temp = "";
    for (int i = 0; i < 2; i++) {
        int nx = x + dr[i];
        int ny = y + dc[i];

        if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
        if (nx == px && ny == py) continue;

        std::string run = dfs(nx, ny, x, y, n);
        if (temp == "" || run < temp) {
            temp = run;
        }
    }

    return memo[x][y] = res + temp;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n,k;
    std::cin >> n >> k;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            std::cin >> grid[i][j];
        }
    }

    std::string res = dfs(0, 0, -1, -1, n);

    for (int i = 0; i < res.size() && k > 0; i++) {
        if (res[i] == 'a') continue;
        res[i] = 'a';
        k--;
    }

    std::cout << res << "\n";
}


#include <iostream>
#include <string>
#include <vector>

#define MAX 2005

typedef std::pair<int, int> pii;
typedef std::pair<pii, pii> pp;

char grid[MAX][MAX];
int vis[MAX][MAX] = {{0}};
int dr[] = {0, 1};
int dc[] = {1, 0};

std::string dfs(int x, int y, int n) {
    std::string res;
    vis[x][y] = 1;
    res.push_back(grid[x][y]);

    std::string temp = "";

    std::vector<pii> mv;
    for (int i = 0; i < 2; i++) {
        int nx = x + dr[i];
        int ny = y + dc[i];

        if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
        if (vis[nx][ny]) continue;

        mv.push_back({nx, ny});
    }

    if (mv.size() > 1 && grid[mv[0].first][mv[0].second] == grid[mv[1].first][mv[1].second]) {
        for (int i = 0; i < 2; i++) {
            std::string run = dfs(mv[i].first, mv[i].second, n);
            if (temp == "" || run < temp) {
                temp = run;
            }
        }
    } else if (mv.size() > 1) {
        pii ff = mv[0]; pii ss = mv[1];

        if (grid[ff.first][ff.second] < grid[ss.first][ss.second]) {
            temp = dfs(ff.first, ff.second, n);
        } else {
            temp = dfs(ss.first, ss.second, n);
        }

    } else if (mv.size() == 1) {
        temp = dfs(mv[0].first, mv[0].second, n);
    }

    vis[x][y] = 0;
    return res + temp;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n,k;
    std::cin >> n >> k;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            std::cin >> grid[i][j];
        }
    }

    std::string res = dfs(0, 0, n);

    for (int i = 0; i < res.size() && k > 0; i++) {
        if (res[i] == 'a') continue;
        res[i] = 'a';
        k--;
    }

    std::cout << res << "\n";
}


#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>

#define MAX 2005
#define INF 1e9

typedef std::pair<int, int> pii;

char grid[MAX][MAX];
int dist[MAX][MAX];

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n,k;
    std::cin >> n >> k;

    for (int i = 0; i < n; i++) {
        std::cin >> grid[i];
    }

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            dist[i][j] = INF;

    int md = 0;
    dist[0][0] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i != 0) {
                dist[i][j] = std::min(dist[i][j], dist[i-1][j]);
            }

            if (j != 0) {
                dist[i][j] = std::min(dist[i][j], dist[i][j-1]);
            }

            if (grid[i][j] != 'a')
                dist[i][j]++;

            md = std::max(md, dist[i][j]);
        }
    }

    int mpref = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (dist[i][j] <= k)
                mpref = std::max(mpref, i + j + 1);
        }
    }

    std::vector<pii> q;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if ((i + j) == mpref && dist[i][j] <= (k + 1))
                q.push_back({i, j});
        }
    }

    std::string suf;
    while (!q.empty()) {
        char best = 'z';

        std::vector<pii> cache;
        for (pii p : q) {
            if (grid[p.first][p.second] <= best) {
                if (grid[p.first][p.second] < best) {
                    cache.clear();
                }
                best = grid[p.first][p.second];
//                std::cout << best << " " << p.first << " " << p.second << "\n";
                for (int dx : {0,1}) {
                    int dy = 1 - dx;
                    int nx = p.first + dx;
                    int ny = p.second + dy;

                    if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
                    cache.push_back({nx, ny});
                }
            }
        }

        suf.push_back(best);
        q = cache;

        sort(q.begin(), q.end());
        q.erase(unique(q.begin(), q.end()), q.end());
    }

    std::string pref;
    for (int i = 0; i < mpref; i++) {
        pref.push_back('a');
    }

    std::cout << (pref+suf) << "\n";
}


#include <iostream>
#include <string>
#include <cstring>

#define MAX 2005

char grid[MAX][MAX];
int vis[MAX][MAX] = {{0}};
int dr[] = {0, 1};
int dc[] = {1, 0};
std::string memo[MAX][MAX];

std::string dfs(int x, int y, int n) {
    std::string res;
    vis[x][y] = 1;
    res.push_back(grid[x][y]);

    if (memo[x][y] != "") return memo[x][y];

    std::string temp = "";
    for (int i = 0; i < 2; i++) {
        int nx = x + dr[i];
        int ny = y + dc[i];

        if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
        if (vis[nx][ny]) continue;

        std::string run = dfs(nx, ny, n);
        if (temp == "" || run < temp) {
            temp = run;
        }
    }

    vis[x][y] = 0;
    return memo[x][y] = res + temp;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n,k;
    std::cin >> n >> k;

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            std::cin >> grid[i][j];
        }
    }

    std::string res = dfs(0, 0, n);

    for (int i = 0; i < res.size() && k > 0; i++) {
        if (res[i] == 'a') continue;
        res[i] = 'a';
        k--;
    }

    std::cout << res << "\n";
}


#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>

#define MAX 2005

typedef std::pair<int, int> pii;

char grid[MAX][MAX];
int dist[MAX][MAX];

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n,k;
    std::cin >> n >> k;

    for (int i = 0; i < n; i++) {
        std::cin >> grid[i];
    }

    dist[0][0] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            dist[i][j] = i + j;

            if (i != 0) {
                dist[i][j] = std::min(dist[i][j], dist[i-1][j]);
            }

            if (j != 0) {
                dist[i][j] = std::min(dist[i][j], dist[i][j-1]);
            }

            if (grid[i][j] != 'a')
                dist[i][j]++;
        }
    }

    int mpref = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (dist[i][j] == k)
                mpref = std::max(mpref, i + j + 1);
        }
    }

    std::vector<pii> q;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if ((i + j) == mpref && dist[i][j] <= (k + 1))
                q.push_back({i, j});
        }
    }

    std::string suf;
    while (!q.empty()) {
        char best = 'z';

        std::vector<pii> cache;
        for (pii p : q) {
            if (grid[p.first][p.second] <= best) {
                if (grid[p.first][p.second] < best) {
                    cache.clear();
                }
                best = grid[p.first][p.second];
//                std::cout << best << " " << p.first << " " << p.second << "\n";
                for (int dx : {0,1}) {
                    int dy = 1 - dx;
                    int nx = p.first + dx;
                    int ny = p.second + dy;

                    if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
                    cache.push_back({nx, ny});
                }
            }
        }

        suf.push_back(best);
        q = cache;

        sort(q.begin(), q.end());
        q.erase(unique(q.begin(), q.end()), q.end());
    }

    std::string pref;
    for (int i = 0; i < mpref; i++) {
        pref.push_back('a');
    }

    std::cout << (pref+suf) << "\n";
}


#include<iostream>
#include<string>
using namespace std;
int main()
{

string s1,s2;
cin>>s1>>s2;
int d[26][10101],a[26]={0};
for(int i=0;i<s1.size();i++)
   d[s1[i]-'a'][a[s1[i]-'a']++]=i;

int x=-1,ans=1;
for(int i=0;i<s2.size();i++)
   {
    if(!a[s2[i]-'a']) {
                       puts("-1");
                       return 0;
                      }
    int p=s2[i]-'a';
    int l=-1,r=a[p],mid=(l+r)/2;
    while(r-1>l)
        {
         if(d[p][mid]<=x) l=mid;
                     else r=mid;
         mid=(l+r)/2;
        }
    if(r<a[p]) x=d[p][r];
          else {
                ans++;
                x=d[p][0];
               }
   }
cout<<ans;

return 0;
}


#include<iostream>
#include<string>
#include<cstdio>
using namespace std;

string s1,s2;
int next[10101][26];
int sz1,sz2;
int ans,now;
int main()
{

cin>>s1>>s2;
sz1=s1.size();
sz2=s2.size();
for(int i=0;i<26;i++)
    next[sz1][i]=-1;
for(int i=sz1-1;i>=0;i--)
    {
     for(int j=0;j<26;j++)
         next[i][j]=next[i+1][j];
     next[i][s1[i]-'a']=i;
    }

ans++;
for(int i=0;i<sz2;i++)
   {
    if(next[now][s2[i]-'a']==-1)
        {
         now=0;
         ans++;
        }
    if(next[now][s2[i]-'a']==-1)
        {
         puts("-1");
         return 0;
        }
    now=next[now][s2[i]-'a']+1;
   }
cout<<ans;

return 0;
}


#include<iostream>
#include<string>
using namespace std;

string s;
int ans,cnt;
int main()
{
string s1,s2;
cin>>s1>>s2;
int s1z=s1.size(),s2z=s2.size();
int a[30]={0};
int b[30]={0};
for(int i=0;i<s1z;i++)
    a[s1[i]-'a']++;
for(int i=0;i<s2z;i++)
    b[s2[i]-'a']++;
for(int i=0;i<30;i++)
    if(!a[i] and b[i]) {
                        puts("-1");
                        return 0;
                       }

int t=0;
for(int i=0;i<s2z;i++)
    if(s2[i]!=s2[0]) t=1;
if(!t) {
        cout<<(s2z+a[s2[0]-'a']-1)/a[s2[0]-'a'];
        return 0;
       }

while(1)
    {
     s+=s1[ans%s1z];
     ans++;
     if(s[ans-1]==s2[cnt]) cnt++;
     if(cnt==s2z) break;
    }
cout<<(ans+s1z-1)/s1z;

return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    string s1, s2;
    cin >> s1 >> s2;
    int ans = 0;
    int idx[10005][26], n = s1.length();
    for (int i = 0; i < 26; i++) idx[n - 1][i] = n;
    for (int i = 0; i < 26; i++) {
        char ch = 'a' + i;
        for (int j = n - 2; j >= 0; j--) {
            if (s1[j + 1] == ch) idx[j][i] = j + 1;
            else idx[j][i] = idx[j + 1][i];
        }
    }
    int j = 0;
    while (j < s2.length()) {
        int jPrev = j;
        for (int i = 0; i < s1.length() && j < s2.length(); ) {
            if (s1[i] == s2[j]) j++, i++;
            else
                i = idx[i][s2[j] - 'a'];
        }
        if (jPrev == j) break;
        ans++;
    }
    if (j < s2.length()) cout << "-1\n";
    else cout << ans << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    string s1, s2;
    cin >> s1 >> s2;
    int ans = 0;
    int j = 0;
    while (j < s2.length()) {
        int jPrev = j;
        for (int i = 0; i < s1.length(); i++) {
            if (s1[i] == s2[j]) j++;
        }
        if (jPrev == j) break;
        ans++;
    }
    if (j < s2.length()) cout << "-1\n";
    else cout << ans << "\n";
    return 0;
}


#pragma GCC optimize(2)

#include<bits/stdc++.h>
#define mp make_pair
#define rep(i,n) for(int i=0;i<n;i++)
#define BINF 0x7fffffff
#define INF 0x3f3f3f3f
#define LINF 9223372036854775807
#define END(s) {cout<<s;return 0;}
#define CON(s) {cout<<s;continue;}
#define BRE(s) {cout<<s;break;}
#define pb push_back
#define All(a) a.begin(),a.end()  //with A in CAPITAL!!!
#define sz(a) (int)a.size()
#define F first
#define S second
using namespace std;

char s1[10004],s2[1000006];
int dis[10004][30];

int main(){
	ios::sync_with_stdio(false);
	gets(s1);
	gets(s2);
	int n=strlen(s1),m=strlen(s2);
	rep(i,26){
		dis[n][i]=-1;
	}
	for(int i=n-1;i>=0;i--){
		rep(j,26){
			dis[i][j]=dis[i+1][j];
		}
		dis[i][s1[i]-'a']=i;
	}
	rep(i,m){
		if(dis[0][s2[i]-'a']==-1){
			puts("-1");
			return 0;
		}
	}
	int i=0,res=1;
	rep(j,m){
		i=dis[i][s2[j]-'a'];
		if(i==-1){
			i=dis[0][s2[j]-'a']+1;
			res++;
		} else i++;
	}
	cout<<res<<endl;
	return 0;
}


#pragma GCC optimize(2)

#include<bits/stdc++.h>
#define mp make_pair
#define rep(i,n) for(int i=0;i<n;i++)
#define BINF 0x7fffffff
#define INF 0x3f3f3f3f
#define LINF 9223372036854775807
#define END(s) {cout<<s;return 0;}
#define CON(s) {cout<<s;continue;}
#define BRE(s) {cout<<s;break;}
#define pb push_back
#define All(a) a.begin(),a.end()  //with A in CAPITAL!!!
#define sz(a) (int)a.size()
#define F first
#define S second
using namespace std;

char s1[10004],s2[1000006];
int l1,l2;

bool check(int x){
	int l=0;
	rep(i,x){
		rep(j,l2){
			if(s2[l]==s1[j]) l++;
		}
	}
	return l==l2;
}

int main(){
	ios::sync_with_stdio(false);
	/*
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	*/
	gets(s1);
	gets(s2);
	l1=strlen(s1),l2=strlen(s2);
	if(!check(l2)) cout<<-1<<endl;
	else{
		int l=1,r=l2,mid;
		while((l+1)<l2){
			mid=(l+r)>>1;
			if(check(mid)) l=mid+1;
			else r=mid-1;
		}
		cout<<mid<<endl;
	}
	return 0;
}


#pragma GCC optimize(2)

#include<bits/stdc++.h>
#define mp make_pair
#define rep(i,n) for(int i=0;i<n;i++)
#define BINF 0x7fffffff
#define INF 0x3f3f3f3f
#define LINF 9223372036854775807
#define END(s) {cout<<s;return 0;}
#define CON(s) {cout<<s;continue;}
#define BRE(s) {cout<<s;break;}
#define pb push_back
#define All(a) a.begin(),a.end()  //with A in CAPITAL!!!
#define sz(a) (int)a.size()
#define F first
#define S second
using namespace std;

char s1[10004],s2[1000006];
int l1,l2;

bool check(int x){
	int j=0;
	rep(i,x){
		rep(k,l1){
			if(s2[j]==s1[k]) j++;
		}
	}
	return j==l2;
}

int main(){
	ios::sync_with_stdio(false);
	/*
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	*/
	gets(s1);
	gets(s2);
	l1=strlen(s1),l2=strlen(s2);
	if(!check(l2)) cout<<-1<<endl;
	else{
		int l=1,r=l2-1,mid;
		while((l+1)<l2){
			mid=(l+r)>>1;
			if(check(mid)) l=mid+1;
			else r=mid-1;
		}
		cout<<mid<<endl;
	}
	return 0;
}


#pragma GCC optimize(2)

#include<bits/stdc++.h>
#define mp make_pair
#define rep(i,n) for(int i=0;i<n;i++)
#define BINF 0x7fffffff
#define INF 0x3f3f3f3f
#define LINF 9223372036854775807
#define END(s) {cout<<s;return 0;}
#define CON(s) {cout<<s;continue;}
#define BRE(s) {cout<<s;break;}
#define pb push_back
#define All(a) a.begin(),a.end()  //with A in CAPITAL!!!
#define sz(a) (int)a.size()
#define F first
#define S second
using namespace std;

char s1[10004],s2[1000006];
int dis[10004][30];

int main(){
	ios::sync_with_stdio(false);
	gets(s1);
	gets(s2);
	int n=strlen(s1),m=strlen(s2);
	rep(i,26){
		dis[n][i]=-1;
	}
	for(int i=n-1;i>=0;i--){
		rep(j,26){
			dis[i][j]=dis[i+1][j];
		}
		dis[i][s1[i]-'a']=i;
	}
	rep(i,m){
		if(dis[0][s2[i]-'a']==-1){
			puts("-1");
			return 0;
		}
	}
	int i=0,res=1;
	rep(j,m){
		i=dis[i][s2[j]-'a'];
		if(i==-1){
			i=dis[0][s2[j]]+1;
			res++;
		} else i++;
	}
	cout<<res<<endl;
	return 0;
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <cstdio>
#include <unordered_map>
#define EXP 2.71828
#define pi 3.1415926536
#define mod 1000000007
typedef long long ll;
using namespace std;
int main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);
    //cout.tie(0);
    string a,b;
    cin >> a >> b;
    int bf[26] = {0},an = 0;
    map< char,vector<int> > f;
    for(int i = 0;i < a.size();i++)f[a[i]].push_back(i);
    for(int i = 0;i < b.size();i++)bf[b[i]-'a']++;
    for(int i = 0;i < b.size();i++)
    {
        char c = b[i];
        if(bf[c-'a']<=0)continue;
        if(f[c].size()==0)return cout << -1,0;
        int in = f[c][0];
        for(char u = 'a';u <= 'z';u++)
        {
            int k = lower_bound(f[u].begin(),f[u].end(),in)-f[u].begin();
            bf[u-'a'] -= int(f[u].size())-k;
        }
        an++;
    }
    cout << an;
    return 0;
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <cstdio>
#include <unordered_map>
#define EXP 2.71828
#define pi 3.1415926536
#define mod 1000000007
typedef long long ll;
using namespace std;
string a,b;
map< char,vector<int> > f;
bool ch(int x)
{
    int cr = b.size()+1,head = 0;
    for(int i = 0;i < b.size();i++)
    {
        char c = b[i];
        if(f[c].size()==0)return 0;
        int k = lower_bound(f[c].begin(),f[c].end(),cr)-f[c].begin();
        if(k==f[c].size())
        {
            head++;
            cr = f[c][0];
        }
        else if(f[c][k]==cr)
        {
            if(k==f[c].size()-1)
            {
                head++;
                cr = f[c][0];
            }
            else cr = f[c][k+1];
        }
        else cr = k;
        if(head > x)return 0;
    }
    return 1;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> a >> b;
    if(a==b)return cout << 1,0;
    for(int i = 0;i < a.size();i++)f[a[i]].push_back(i);
    if(!ch(1000000))return cout << -1,0;
    int l = 2,h = 1000000,mid;
    while(true)
    {
        mid =(l+h)/2;
        bool e = ch(mid),e2 = ch(mid-1);
        if(e&&!e2)break;
        if(e)h = mid-1;
        else l = mid+1;
        //cout << mid <<" "<<e<<endl;
    }
    cout << mid;
    return 0;
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <cstdio>
#include <unordered_map>
#define EXP 2.71828
#define pi 3.1415926536
#define mod 1000000007
typedef long long ll;
using namespace std;
string a,b;
map< char,vector<int> > f;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> a >> b;
    if(a==b)return cout << 1,0;
    for(int i = 0;i < a.size();i++)f[a[i]].push_back(i);
    int cr = 1e7,head = 0;
    for(int i = 0;i < b.size();i++)
    {
        char c = b[i];
        if(f[c].size()==0)return cout << -1,0;
        int k = lower_bound(f[c].begin(),f[c].end(),cr)-f[c].begin();
        if(k==f[c].size())
        {
            head++;
            cr = f[c][0];
        }
        else if(f[c][k]==cr)
        {
            if(k==f[c].size()-1)
            {
                head++;
                cr = f[c][0];
            }
            else cr = f[c][k+1];
        }
        else cr = f[c][k];
    }
    cout << head;
    return 0;
}


#include <bits/stdc++.h>
#include <stdio.h>
#include <cstdio>
#include <unordered_map>
#define EXP 2.71828
#define pi 3.1415926536
#define mod 1000000007
typedef long long ll;
using namespace std;
string a,b;
map< char,vector<int> > f;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> a >> b;
    if(a==b)return cout << 1,0;
    for(int i = 0;i < a.size();i++)f[a[i]].push_back(i);
    int cr = 1e7,head = 0;
    for(int i = 0;i < b.size();i++)
    {
        char c = b[i];
        if(f[c].size()==0)return 0;
        int k = lower_bound(f[c].begin(),f[c].end(),cr)-f[c].begin();
        if(k==f[c].size())
        {
            head++;
            cr = f[c][0];
        }
        else if(f[c][k]==cr)
        {
            if(k==f[c].size()-1)
            {
                head++;
                cr = f[c][0];
            }
            else cr = f[c][k+1];
        }
        else cr = f[c][k];
    }
    cout << head;
    return 0;
}


#include<bits/stdc++.h>

#define  ll         long long
#define  llu        unsigned long long
#define  mod        1000000000+7
#define  pi         ((2.0)*acos(0.0))
#define  pb         push_back
#define  pf         push_front
#define  mp         make_pair
#define  fr0(i,n)   for(ll i=0;i<n;i++)
#define  fr1(i,n)   for(ll i=1;i<=n;i++)

using namespace std;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    string s,str;
    cin>>s>>str;

    ll cnt=0;
    for(ll i=0;str[i]!='\0';i++)
    {
        bool check=false;
         bool flag=true;
        for(ll j=0;s[j]!='\0';j++)
        {
            if(s[j]==str[i])
            {
                i++;
                check=true;
                flag=false;
            }
            else if(check && s[j]!=str[i])
                break;
        }
        i--;
        if(flag) {cnt=-1;break;}
        cnt++;
    }
    cout<<cnt<<"\n";
return 0;
}


#include<bits/stdc++.h>

#define  ll         long long
#define  llu        unsigned long long
#define  mod        1000000000+7
#define  pi         ((2.0)*acos(0.0))
#define  pb         push_back
#define  pf         push_front
#define  mp         make_pair
#define  fr0(i,n)   for(ll i=0;i<n;i++)
#define  fr1(i,n)   for(ll i=1;i<=n;i++)

using namespace std;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    string s,str;
    cin>>s>>str;

    ll cnt=0;
    for(ll i=0;str[i]!='\0';i++)
    {
        bool check=false;
         bool flag=true;
        for(ll j=0;s[j]!='\0';j++)
        {
            if(s[j]==str[i])
            {
                i++;
                check=true;
                flag=false;
            }
            if(check && s[j]!=str[i])
                break;
        }
        i--;
        if(flag) {cnt=-1;break;}
        cnt++;
    }
    cout<<cnt<<"\n";
return 0;
}


#include<bits/stdc++.h>

#define  ll         long long
#define  llu        unsigned long long
#define  mod        1000000007
#define  pi         ((2.0)*acos(0.0))
#define  pb         push_back
#define  pf         push_front
#define  eb         emplace_back
#define  mp         make_pair
#define  all(x)     (x).begin(),(x).end()
#define  X          first
#define  Y          second

/*loop*/
#define  fr(i,m,n)   for(ll i=m;i<n;++i)

/*output shortcuts*/
#define  nl         "\n"
#define  Yes        cout<<"Yes\n"
#define  yes        cout<<"yes\n"
#define  YES        cout<<"YES\n"
#define  No         cout<<"No\n"
#define  no         cout<<"no\n"
#define  NO         cout<<"NO\n"

using namespace std;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    string s1,s2;
    cin>>s1>>s2 ;
    map<ll,vector<ll>>mp;
    fr(i,0,s1.size())
    {
        ll x= s1[i]-'a';
        mp[x].pb(i);
    }
    ll cnt=0;
    ll sz=s2.size();
    ll flag=0;
    fr(i,0,sz-1)
    {
        ll x = s2[i]-'a';
        ll y = s2[i+1]-'a';

        auto it1=mp.find(x);
        auto it2=mp.find(y);

        if(it1!=mp.end() && it2!=mp.end()){
        ll z = mp[y].size();
        if(mp[x][0] >= mp[y][z-1])
            cnt++;
        }
        else
        {
            flag=1;
            break;
        }
    }
    if(!flag)
        cout<<cnt+1<<nl;
    else
        cout<<"-1"<<nl;
return 0;
}


#include<bits/stdc++.h>

#define  ll         long long
#define  llu        unsigned long long
#define  mod        1000000000+7
#define  pi         ((2.0)*acos(0.0))
#define  pb         push_back
#define  pf         push_front
#define  mp         make_pair
#define  fr0(i,n)   for(ll i=0;i<n;i++)
#define  fr1(i,n)   for(ll i=1;i<=n;i++)

using namespace std;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    string s,str;
    cin>>s>>str;

    ll cnt=0;
    for(ll i=0;str[i]!='\0';i++)
    {
        bool check=false;
         bool flag=true;
        for(ll j=0;s[j]!='\0';j++)
        {
            if(s[j]==str[i])
            {
                i++;
                check=true;
                flag=false;
            }
        }
        i--;
        if(flag) {cnt=-1;break;}
        cnt++;
    }
    cout<<cnt<<"\n";
return 0;
}


#include<bits/stdc++.h>

#define  ll         long long
#define  llu        unsigned long long
#define  mod        1000000007
#define  pi         ((2.0)*acos(0.0))
#define  pb         push_back
#define  pf         push_front
#define  eb         emplace_back
#define  mp         make_pair
#define  all(x)     (x).begin(),(x).end()
#define  X          first
#define  Y          second

/*loop*/
#define  fr(i,m,n)   for(ll i=m;i<n;++i)

/*output shortcuts*/
#define  nl         "\n"
#define  Yes        cout<<"Yes\n"
#define  yes        cout<<"yes\n"
#define  YES        cout<<"YES\n"
#define  No         cout<<"No\n"
#define  no         cout<<"no\n"
#define  NO         cout<<"NO\n"

using namespace std;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    string s1,s2;
    cin>>s1>>s2 ;
    vector<ll>v[26];
    fr(i,0,s1.size())
    {
        ll x= s1[i]-'a';
        v[x].pb(i);
    }
    ll flag=-1,cnt=0,val=1;

    fr(i,0,s2.size())
    {
        ll x=s2[i]-'a';
        if(v[x].empty())
        {
            val=0;
            break;
        }
        else
        {
            auto it=upper_bound(all(v[x]),flag);
            if(it!=v[x].end())
                flag=*it;
            else
            {
                flag=-1;
                cnt++;
            }
        }
    }
    if(!val) cnt=-2;
    cout<<cnt+1<<nl;
return 0;
}


#include<bits/stdc++.h>

#define  ll         long long
#define  llu        unsigned long long
#define  mod        1000000007
#define  pi         ((2.0)*acos(0.0))
#define  pb         push_back
#define  pf         push_front
#define  eb         emplace_back
#define  mp         make_pair
#define  all(x)     (x).begin(),(x).end()
#define  X          first
#define  Y          second

/*loop*/
#define  fr(i,m,n)   for(ll i=m;i<n;++i)

/*output shortcuts*/
#define  nl         "\n"
#define  Yes        cout<<"Yes\n"
#define  yes        cout<<"yes\n"
#define  YES        cout<<"YES\n"
#define  No         cout<<"No\n"
#define  no         cout<<"no\n"
#define  NO         cout<<"NO\n"

using namespace std;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    string s1,s2;
    cin>>s1>>s2 ;
    vector<ll>v[26];
    fr(i,0,s1.size())
    {
        ll x= s1[i]-'a';
        v[x].pb(i);
    }
    ll flag=-1,cnt=1,val=1;

    fr(i,0,s2.size())
    {
        ll x=s2[i]-'a';
        if(v[x].empty())
        {
            val=0;
            break;
        }
        else
        {
            auto it=upper_bound(all(v[x]),flag);
            if(it!=v[x].end())
                {
                    //cout<<i<<"it : "<<*it<<nl;
                    //if(*it == 0)
                        //cnt++;
                    flag=*it;
                }
            else
            {
                flag=-1;
                i--;
                //cout<<i<<" ";
                cnt++;
            }
        }
    }
    if(!val) cnt=-1;
    cout<<cnt<<nl;
return 0;
}


#include<bits/stdc++.h>

#define  ll         long long
#define  llu        unsigned long long
#define  mod        1000000000+7
#define  pi         ((2.0)*acos(0.0))
#define  pb         push_back
#define  pf         push_front
#define  mp         make_pair
#define  fr0(i,n)   for(ll i=0;i<n;i++)
#define  fr1(i,n)   for(ll i=1;i<=n;i++)

using namespace std;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    string s,str;
    cin>>s>>str;

    ll cnt=0;
    for(ll i=0;str[i]!='\0';i++)
    {
        //bool check=false;
         bool flag=true;
        for(ll j=0;s[j]!='\0';j++)
        {
            if(s[j]==str[i])
            {
                i++;
                //check=true;
                flag=false;
            }
        }
        i--;
        if(flag) {cnt=-1;break;}
        cnt++;
    }
    cout<<cnt<<"\n";
return 0;
}


#include<bits/stdc++.h>

#define  ll         long long
#define  llu        unsigned long long
#define  mod        1000000007
#define  pi         ((2.0)*acos(0.0))
#define  pb         push_back
#define  pf         push_front
#define  eb         emplace_back
#define  mp         make_pair
#define  all(x)     (x).begin(),(x).end()
#define  X          first
#define  Y          second

/*loop*/
#define  fr(i,m,n)   for(ll i=m;i<n;++i)

/*output shortcuts*/
#define  nl         "\n"
#define  Yes        cout<<"Yes\n"
#define  yes        cout<<"yes\n"
#define  YES        cout<<"YES\n"
#define  No         cout<<"No\n"
#define  no         cout<<"no\n"
#define  NO         cout<<"NO\n"

using namespace std;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    string s1,s2;
    cin>>s1>>s2 ;
    vector<ll>v[26];
    fr(i,0,s1.size())
    {
        ll x= s1[i]-'a';
        v[x].pb(i);
    }
    ll flag=-1,cnt=1,val=1;

    fr(i,0,s2.size())
    {
        ll x=s2[i]-'a';
        if(v[x].empty())
        {
            val=0;
            break;
        }
        else
        {
            auto it=upper_bound(all(v[x]),flag);
            if(it!=v[x].end())
                {
                    //cout<<i<<"it : "<<*it<<nl;
                    if(*it == 0)
                        cnt++;
                    flag=*it;
                }
            else
            {
                flag=-1;
                //cout<<i<<" ";
                cnt++;
            }
        }
    }
    if(!val) cnt=-1;
    cout<<cnt<<nl;
return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#define INF 1e9
#define ll long long
#define ms(a,val) memset(a,val,sizeof(a))
#define mc(a,b) memcpy(b,a,sizeof(a))
#define MOD 1000000007

using namespace std;
int nexts[10005][26];
void makenext(string str) {
	for (int i = 0;i < str.length();i++) {
		for (int j = 0;j < 26;j++) {
			nexts[i][j] = str.length();
		}
	}
	for (int i = str.length() - 1;i > 0;i--) {
		mc(nexts[i], nexts[i - 1]);
		nexts[i-1][str[i] - 'a'] = i;
	}
}
int main() {
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	string s1, s2;
	while (cin >> s1 >> s2) {
		makenext(s1);
		int pos = 0,tpos=0,sum=0,flag=0;
		for (int i = 0;i < s2.length();i++) {
			if (tpos == s1.length())sum++, pos=tpos = 0;
			if (s1[pos] != s2[i]) {
				tpos = nexts[pos][s2[i] - 'a'];
				if (pos == 0 && tpos == s1.length()) {
					flag = 1;break;
				}
				else if (tpos == s1.length())sum++, tpos = 0, i--;
				else tpos++;
			}
			else {
				tpos++;
				if (tpos == s1.length())sum++, tpos = 0;
			}
			pos = tpos;
		}
		if (flag)cout << -1 << "\n";
		else if (pos)cout << sum + 1 << "\n";
		else cout << sum << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
set<int> ss[30];
char s1[1005];
char s2[1000005];
bool vis[30];
int main()
{
    scanf("%s", s1+1);
    scanf("%s", s2+1);
    int len1=strlen(s1+1);
    int len2=strlen(s2+1);
    for(int i=1; i<=len1; i++)
    {
        ss[s1[i]-'a'].insert(i);
        vis[s1[i]-'a']=1;
    }

    for(int i=1; i<=len2; i++)
    {
        if(vis[s2[i]-'a']==0)
        {
            cout<<-1<<endl;
            return 0;
        }
    }

    int ans=1;
    int pos=0;
    for(int i=1; i<=len2; i++)
    {
        int x=s2[i]-'a';
        auto it=ss[x].upper_bound(pos);
        if(it==ss[x].end())
        {
            pos=0;
            ans++;
            pos=*ss[x].upper_bound(pos);
        }
        else{
            pos=*it;
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const ll INF=1e15+7;
const int maxn=10005;
int n,k;
ll p;
int dis[maxn][26];//从i位置到一下个字符的位置
int vis[26];
int main()
{
    string s1,s2;
    cin>>s1>>s2;
    int len1=s1.length(),len2=s2.length();
    memset(vis,0,sizeof(0));
    memset(dis,-1,sizeof(dis));
    for(int i=0;i<len1;i++)
    {
        vis[s1[i]-'a']=1;
        for(int j=0;j<=i;j++)
        {
            if( dis[j][s1[i]-'a']==-1)
            dis[j][s1[i]-'a']=i;
        }
    }
    for(int i=0;i<len2;i++)
    {
        if(vis[s2[i]-'a']==0)
        {
            cout<<-1<<'\n';
            return 0;
        }
    }
    int ans=0;
    for(int i=0;i<len2;)
    {
        int j=0;
        while(j<len1&&i<len2&&dis[j][s2[i]-'a']!=-1)
        {
            j=dis[j][s2[i]-'a']+1;
            i++;
        }
        ans++;
    }
    if(s1==s2)
        cout<<0<<'\n';
    else
        cout<<ans<<'\n';
    return 0;
}


/**
 * copied
 * Author: Sergey Kopeliovich (Burunduk30@gmail.com)
 */

#include <ctime>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>

#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <functional>
#include <map>
#include <set>
#include <fstream>
#include <queue>

using namespace std;

#ifdef _WIN32
#  define I64 "%I64d"
#else
#  define I64 "%Ld"
#endif

#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define fornd(i, n) for (int i = (int)(n) - 1; i >= 0; i--)
#define forab(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define forabd(i, a, b) for (int i = (int)(b); i >= (int)(a); i--)
#define forit(i, a) for (__typeof((a).begin()) i = (a).begin(); i != (a).end(); i++)
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define zero(a) memset(a, 0, sizeof(a))
#define pb push_back
#define mp make_pair

#define EOL(i, n) " \n"[i == (n) - 1]
#define LEN(a) (int)(sizeof(a) / sizeof(a[0]))
#define IS(a, i) (((a) >> (i)) & 1)

typedef double dbl;
typedef long long ll;
typedef vector <int> vi;
typedef pair <int, int> pii;
typedef unsigned char byte;

template <class T> T inline sqr(T x) { return x * x; }
template <class T> inline void relaxMin( T &a, T b ) { a = min(a, b); }
template <class T> inline void relaxMax( T &a, T b ) { a = max(a, b); }

string str( int i ) { char s[100]; sprintf(s, "%d", i); return string(s); }

template <class T> inline T sign( T x ) { return x > 0 ? 1 : (x < 0 ? -1 : 0); }
template <class T> inline T myAbs( T a ) { return a > 0 ? a : -a; }

const int mlen = (int)1e6 + 10;

char s[mlen];
char t[mlen];
int sn, tn;
vi pos[26];

int main()
{
  scanf("%s%s", s, t);
  sn = strlen(s);
  tn = strlen(t);
  forn(i, sn)
    pos[s[i] - 'a'].pb(i);
  forn(i, tn)
    if (!sz(pos[t[i] - 'a']))
    {
      puts("-1");
      return 0;
    }

  int p = 0, cnt = 1;
  forn(i, tn)
  {
    vi &v = pos[t[i] - 'a'];
    int j = lower_bound(all(v), p) - v.begin();
    if (j == sz(v))
      cnt++, p = v[0] + 1;
    else
      p = v[j] + 1;
  }
  printf("%d\n", cnt);
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define RANGE 10010
typedef long long int ll;
int arr[RANGE][26];
int hashtb[2][26];
int main(){
    string st1, st2;
    cin>>st1>>st2;
    memset(arr, -1, sizeof arr);
    int num;
    for(int i = 0; i < st2.length(); i++){
        num = st2[i] - 'a';
        hashtb[1][num] = 1;
    }
    num = st1[st1.length() - 1] - 'a';
    hashtb[0][num] = 1;
    arr[st1.length() - 1][num] = st1.length() - 1;
    for(int i = st1.length() - 2; i >= 0; i--){
        num = st1[i] - 'a';
        hashtb[0][num] = 1;
        for(int j = 0; j < 26; j++){
            arr[i][j] = arr[i + 1][j];
        }
        arr[i][num] = i;
    }
    int flag = 0;
    for(int i = 0; i < 26; i++){
        if(hashtb[0][i] != hashtb[1][i]){
            flag = 1;
            break;
        }
    }
    if(flag ==1){
        cout<<"-1\n";
        return 0;
    }
    ll ct = 1;
    int curr = 0;
    for(int i = 0; i < st2.length(); i++){
        num = st2[i] - 'a';
        curr = arr[curr][num];
        if(curr == -1){
            curr = st1.length() - 1;
        }
        curr++;
        if(curr % st1.length() == 0){
            curr = 0;
            ct++;
            if(i == st2.length() - 1){
                ct--;
            }
        }
    }
    cout<<ct<<"\n";
}


#include<bits/stdc++.h>
using namespace std;
#define RANGE 10010
typedef long long int ll;
int arr[RANGE][26];
int hashtb[2][26];
int main(){
    string st1, st2;
    cin>>st1>>st2;
    memset(arr, -1, sizeof arr);
    memset(hashtb, 0, sizeof hashtb);
    int num;
    for(int i = 0; i < st2.length(); i++){
        num = st2[i] - 'a';
        hashtb[1][num] = 1;
    }
    num = st1[st1.length() - 1] - 'a';
    hashtb[0][num] = 1;
    arr[st1.length() - 1][num] = st1.length() - 1;
    for(int i = st1.length() - 2; i >= 0; i--){
        num = st1[i] - 'a';
        hashtb[0][num] = 1;
        for(int j = 0; j < 26; j++){
            arr[i][j] = arr[i + 1][j];
        }
        arr[i][num] = i;
    }
    int flag = 0;
    for(int i = 0; i < 26; i++){
        if(hashtb[1][i] == 1 && hashtb[0][i] != hashtb[1][i]){
            flag = 1;
            break;
        }
    }
    if(flag == 1){
        cout<<"-1\n";
        return 0;
    }
    ll ct = 0;
    int curr = 0;
    for(int i = 0; i < st2.length(); i++){
        num = st2[i] - 'a';
        curr = arr[curr][num];
        if(curr == -1){
            curr = 0;
            ct++;
            i--;
        }
        else if((curr + 1) % st1.length() == 0 && (i != st2.length() - 1)){
            curr = 0;
            ct++;
        }
        else if(i == st2.length() - 1){
            ct++;
        }
        else{
            curr++;
        }
    }
    cout<<ct<<"\n";
}


#include<bits/stdc++.h>
using namespace std;
#define RANGE 10010
typedef long long int ll;
int arr[RANGE][26];
int hashtb[2][26];
int main(){
    string st1, st2;
    cin>>st1>>st2;
    memset(arr, -1, sizeof arr);
    int num;
    for(int i = 0; i < st2.length(); i++){
        num = st2[i] - 'a';
        hashtb[1][num] = 1;
    }
    num = st1[st1.length() - 1] - 'a';
    hashtb[0][num] = 1;
    arr[st1.length() - 1][num] = st1.length() - 1;
    for(int i = st1.length() - 2; i >= 0; i--){
        num = st1[i] - 'a';
        hashtb[0][num] = 1;
        for(int j = 0; j < 26; j++){
            arr[i][j] = arr[i + 1][j];
        }
        arr[i][num] = i;
    }
    int flag = 0;
    for(int i = 0; i < 26; i++){
        if(hashtb[0][i] != hashtb[1][i]){
            flag = 1;
            break;
        }
    }
    if(flag ==1){
        cout<<"-1\n";
        return 0;
    }
    ll ct = 0;
    int curr = 0;
    for(int i = 0; i < st2.length(); i++){
        num = st2[i] - 'a';
        curr = arr[curr][num];
        if(curr == -1){
            curr = 0;
            ct++;
            i--;
        }
        else if((curr + 1) % st1.length() == 0 && (i != st2.length() - 1)){
            curr = 0;
            ct++;
        }
        else if(i == st2.length() - 1){
            ct++;
        }
        else{
            curr++;
        }
    }
    cout<<ct<<"\n";
}


#include<bits/stdc++.h>
using namespace std;
#define RANGE 10010
typedef long long int ll;
int arr[RANGE][26];
int hashtb[2][26];
int main(){
    string st1, st2;
    cin>>st1>>st2;
    memset(arr, -1, sizeof arr);
    int num;
    for(int i = 0; i < st2.length(); i++){
        num = st2[i] - 'a';
        hashtb[1][num] = 1;
    }
    num = st1[st1.length() - 1] - 'a';
    hashtb[0][num] = 1;
    arr[st1.length() - 1][num] = st1.length() - 1;
    for(int i = st1.length() - 2; i >= 0; i--){
        num = st1[i] - 'a';
        hashtb[0][num] = 1;
        for(int j = 0; j < 26; j++){
            arr[i][j] = arr[i + 1][j];
        }
        arr[i][num] = i;
    }
    int flag = 0;
    for(int i = 0; i < 26; i++){
        if(hashtb[0][i] != hashtb[1][i]){
            flag = 1;
            break;
        }
    }
    if(flag ==1){
        cout<<"-1\n";
        return 0;
    }
    ll ct = 1;
    int curr = 0;
    for(int i = 0; i < st2.length(); i++){
        num = st2[i] - 'a';
        curr = arr[curr][num];
        if(curr == -1){
            curr = st1.length() - 1;
        }
        curr++;
        if(curr % st1.length() == 0){
            curr = 0;
            ct++;
        }
    }
    cout<<ct<<"\n";
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class SolutionE extends Thread {
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

    private static final FastReader scanner = new FastReader();
    private static final PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) {
        new Thread(null, new SolutionE(), "Main", 1 << 26).start();
    }

    public void run() {
        int t = 1;
        for (int i = 0; i < t; i++) {
            solve();
        }
        out.close();
    }

    private static void solve() {
        int n = scanner.nextInt();
        int p = scanner.nextInt();
        Integer[] a = new Integer[n];
        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }
        Arrays.sort(a);

        int minXForWin = 0;
        for (int i = 0; i < n; i++) {
            minXForWin = Math.max(minXForWin, a[i] - i);
        }

        int low = 0;
        int high = 1_200_000_000;

        while (high - low > 1) {
            int mid = (low + high) / 2; //start Candies

            int amountCandies = mid;
            int amountEnemiesDefeated = 0;
            boolean isGood = amountCandies < a[n-1];
            for (int i = 0; i < n; i++) {
                if (a[i] > amountCandies && i - amountEnemiesDefeated >= p) {
                    isGood = false;
                } else if (a[i] > amountCandies) {
                    amountEnemiesDefeated++;
                    amountCandies++;
                }
            }

            if (isGood) {
                low = mid;
            } else {
                high = mid;
            }
        }
        System.out.println(Math.max(0, high - minXForWin));
        System.out.println(IntStream.range(minXForWin, high).mapToObj(String::valueOf).collect(Collectors.joining(" ")));
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class SolutionE extends Thread {
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

    private static final FastReader scanner = new FastReader();
    private static final PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) {
        new Thread(null, new SolutionE(), "Main", 1 << 26).start();
    }

    public void run() {
        int t = 1;
        for (int i = 0; i < t; i++) {
            solve();
        }
        out.close();
    }

    private static void solve() {
        int n = scanner.nextInt();
        int p = scanner.nextInt();
        Integer[] a = new Integer[n];
        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }
        Arrays.sort(a);

        int minXForWin = 0;
        for (int i = 0; i < n; i++) {
            minXForWin = Math.max(minXForWin, a[i] - i);
        }

        int low = 0;
        int high = 1_200_000_000;

        while (high - low > 1) {
            int mid = (low + high) / 2; //start Candies

            int amountCandies = mid;
            int amountEnemiesDefeated = 0;
            boolean isGood = amountCandies < a[n-1];
            for (int i = 0; i < n; i++) {
                if (a[i] > amountCandies && i - amountEnemiesDefeated >= p) {
                    isGood = false;
                } else if (a[i] > amountCandies) {
                    amountEnemiesDefeated++;
                    amountCandies++;
                }
            }

            if (isGood) {
                low = mid;
            } else {
                high = mid;
            }
        }
        System.out.println(high - minXForWin);
        System.out.println(IntStream.range(minXForWin, high).mapToObj(String::valueOf).collect(Collectors.joining(" ")));
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class SolutionE extends Thread {
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

    private static final FastReader scanner = new FastReader();
    private static final PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) {
        new Thread(null, new SolutionE(), "Main", 1 << 26).start();
    }

    public void run() {
        int t = 1;
        for (int i = 0; i < t; i++) {
            solve();
        }
        out.close();
    }

    private static void solve() {
        int n = scanner.nextInt();
        int p = scanner.nextInt();
        Integer[] a = new Integer[n];
        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }
        Arrays.sort(a);

        int minXForWin = 0;
        for (int i = 0; i < n; i++) {
            minXForWin = Math.max(minXForWin, a[i] - i);
        }

        int low = minXForWin - 2;
        int high = 1_100_000_001;

        while (high - low > 1) {
            int mid = (low + high) / 2; //start Candies

            int amountCandies = mid;
            int amountEnemiesDefeated = 0;
            boolean isGood = amountCandies < a[n-1];
            for (int i = 0; i < n; i++) {
                if (a[i] > amountCandies && i - amountEnemiesDefeated >= p) {
                    isGood = false;
                } else if (a[i] > amountCandies) {
                    amountEnemiesDefeated++;
                    amountCandies++;
                }
            }

            if (isGood && n - amountEnemiesDefeated < p) {
                low = mid;
            } else {
                high = mid;
            }
        }
        System.out.println(Math.max(0, high - minXForWin));
        System.out.println(IntStream.range(minXForWin, high).mapToObj(String::valueOf).collect(Collectors.joining(" ")));
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class SolutionE extends Thread {
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

    private static final FastReader scanner = new FastReader();
    private static final PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) {
        new Thread(null, new SolutionE(), "Main", 1 << 26).start();
    }

    public void run() {
        int t = 1;
        for (int i = 0; i < t; i++) {
            solve();
        }
        out.close();
    }

    private static void solve() {
        int n = scanner.nextInt();
        int p = scanner.nextInt();
        Integer[] a = new Integer[n];
        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }
        Arrays.sort(a);

        int minXForWin = 0;
        for (int i = 0; i < n; i++) {
            minXForWin = Math.max(minXForWin, a[i] - i);
        }

        int low = minXForWin - 1;
        int high = 1_200_000_000;

        while (high - low > 1) {
            int mid = (low + high) / 2; //start Candies

            int amountCandies = mid;
            int amountEnemiesDefeated = 0;
            boolean isGood = amountCandies < a[n-1];
            for (int i = 0; i < n; i++) {
                if (a[i] > amountCandies && i - amountEnemiesDefeated >= p) {
                    isGood = false;
                } else if (a[i] > amountCandies) {
                    amountEnemiesDefeated++;
                    amountCandies++;
                }
            }

            if (isGood) {
                low = mid;
            } else {
                high = mid;
            }
        }
        System.out.println(Math.max(0, high - minXForWin));
        System.out.println(IntStream.range(minXForWin, high).mapToObj(String::valueOf).collect(Collectors.joining(" ")));
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.util.stream.Collectors;

public class SolutionE extends Thread {
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

    private static final FastReader scanner = new FastReader();
    private static final PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) {
        new Thread(null, new SolutionE(), "Main", 1 << 26).start();
    }

    public void run() {
        int t = 1;
        for (int i = 0; i < t; i++) {
            solve();
        }
        out.close();
    }

    private static void solve() {
        int n = scanner.nextInt();
        int p = scanner.nextInt();
        Integer[] a = new Integer[n];
        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }
        Arrays.sort(a);

        int minXForWin = 0;
        for (int i = 0; i < n; i++) {
            minXForWin = Math.max(minXForWin, a[i] - i);
        }
        List<Integer> goodIntegers = new ArrayList<>();
        for (int i = 0; i < n-1; i++) {
            if (a[i] < a[i+1] && i + 1 < p) {
                for (int x = Math.max(minXForWin, a[i]); x < a[i+1]; x++) {
                    goodIntegers.add(x);
                }
            }
        }
        System.out.println(goodIntegers.size());
        System.out.println(goodIntegers.stream().map(String::valueOf).collect(Collectors.joining(" ")));
    }
}