Problem: You are given a non-empty line s and an integer k. The following operation is performed with this line exactly once:

  * A line is split into at most k non-empty substrings, i.e. string s is represented as a concatenation of a set of strings s = t1 + t2 + ... + tm, 1 ≤ m ≤ k. 
  * Some of strings ti are replaced by strings tir, that is, their record from right to left. 
  * The lines are concatenated back in the same order, we get string s' = t'1t'2... t'm, where t'i equals ti or tir. 



Your task is to determine the lexicographically smallest string that could be the result of applying the given operation to the string s.

Input

The first line of the input contains string s (1 ≤ |s| ≤ 5 000 000), consisting of lowercase English letters. The second line contains integer k (1 ≤ k ≤ |s|) — the maximum number of parts in the partition.

Output

In the single line print the lexicographically minimum string s' which can be obtained as a result of performing the described operation. 

Examples

Input

aba
2


Output

aab


Input

aaaabacaba
2


Output

aaaaabacab


Input

bababa
1


Output

ababab


Input

abacabadabacaba
4


Output

aababacabacabad

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
const int MAXN = 5000010;
const int INF = 1e9;
char Ans[MAXN + 1];
int zn[MAXN * 2 + 1];
inline void Exkmp(string s) {
  int l = 0;
  for (int i = 1; i < s.length(); i++) {
    if (l + zn[l] > i) zn[i] = min(zn[i - l], l + zn[l] - i);
    while (i + zn[i] < s.length() && s[zn[i]] == s[i + zn[i]]) zn[i]++;
    if (i + zn[i] > l + zn[l]) l = i;
  }
  return;
}
char s[MAXN + 1];
vector<pair<int, int> > S, T;
string st, sr;
int n, N, K;
int f[2][MAXN + 1];
inline void Add(int l, int r) {
  for (int i = l; i <= r; i++) Ans[++n] = s[i];
}
string A;
bool flag = 0;
inline void Update(int dv, int r1, int r2) {
  string tmp;
  tmp.clear();
  if (!r1)
    for (int i = dv; i <= N; i++) tmp += s[i];
  else
    for (int i = N; i >= dv; i--) tmp += s[i];
  if (!r2)
    for (int i = 1; i < dv; i++) tmp += s[i];
  else
    for (int i = dv - 1; i >= 1; i--) tmp += s[i];
  if (!flag || tmp < A) flag = 1, A = tmp;
}
inline void Judge(int p) {
  Update(p, 0, 0);
  Update(p, 0, 1);
}
int id;
int main() {
  scanf("%s", s + 1);
  N = strlen(s + 1);
  reverse(s + 1, s + N + 1);
  K = read();
  if (K == 1) {
    bool rev = 0;
    for (int i = 1; i <= N; i++) {
      if (s[i] > s[N - i + 1]) {
        rev = 1;
        break;
      }
      if (s[i] < s[N - i + 1]) {
        rev = 0;
        break;
      }
    }
    if (rev) reverse(s + 1, s + N + 1);
    puts(s + 1);
    return 0;
  }
  T.push_back(pair<int, int>(0, 0));
  S.push_back(pair<int, int>(0, 0));
  for (int i = 1; i <= N;) {
    int j = i, k = i + 1;
    while (k <= N && s[j] <= s[k]) {
      if (s[j] == s[k])
        ++j;
      else
        j = i;
      ++k;
    }
    int len = k - j;
    ++id;
    while (i <= j) T.push_back(pair<int, int>(i + k - j - 1, id)), i += k - j;
    S.push_back(pair<int, int>(i - 1, id));
  }
  int last = id + 1, pos = 0, lastlen = 99;
  for (int i = T.size() - 1; i > 0; i--) {
    if (last != T[i].second) {
      if (lastlen > 1 || T[i].first - T[i - 1].first > 1) --K;
      if (K == 1) {
        ++K;
        pos = i;
        break;
      }
      last = T[i].second;
      lastlen = T[i].first - T[i - 1].first;
    }
    Add(T[i - 1].first + 1, T[i].first);
  }
  if (!pos) {
    Ans[++n] = '\0';
    puts(Ans + 1);
    return 0;
  }
  N = T[pos].first;
  while (S[S.size() - 1].first > T[pos].first) S.pop_back();
  while (T[T.size() - 1].first > T[pos].first) T.pop_back();
  int dv = T[pos - 1].first + 1, pn = pos - 1;
  while (pn > 0 && T[pn].second == T[pos].second) --pn;
  Judge(N + 1);
  Judge(T[pn].first + 1);
  if (pn >= 1) {
    int j = T[pn - 1].first + 1;
    while (dv < N && s[j] == s[dv]) ++j, ++dv;
    if (dv == N) {
      int lst = pn;
      while (pn > 0 && T[pn].second == T[lst].second) --pn;
      Judge(T[pn].first + 1);
    }
  }
  reverse(s + 1, s + N + 1);
  string st, sr, sn;
  for (int i = 1; i <= N; i++) st += s[i];
  for (int i = 1; i <= N; i++) sr += s[N - i + 1];
  sn = sr + st;
  Exkmp(sn);
  f[1][1] = N;
  for (int i = 2; i <= N; i++) f[1][i] = min(zn[i - 1], N - (i - 1));
  for (int i = 1; i <= N; i++) f[0][i] = min(zn[i + N - 1], N - (i - 1));
  int j = 1;
  for (int i = 2; i <= N; i++) {
    if (f[0][j + 1] < i - j) {
      if (s[f[0][j + 1] + j + 1] < s[N - f[0][j + 1]]) j = i;
    } else {
      if (f[1][(i - j) + 1] < (N - j) - (i - j) &&
          s[N - (i - j) - f[1][(i - j) + 1]] > s[N - f[1][(i - j) + 1]])
        j = i;
    }
  }
  reverse(s + 1, s + N + 1);
  Update(N - j + 1, 1, 0);
  Ans[++n] = '\0';
  printf("%s", Ans + 1);
  for (int i = 0; i < A.length(); i++) printf("%c", A[i]);
  printf("\n");
  return 0;
}