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;
const int N = 5005000;
int n, m, K, fk[N], le[N], nx[N], ex[N];
char S[N], T[N];
struct ele {
  char s[N];
  int n;
  void addl(char* t, int L) {
    for (int i = 0; i < L; i++) s[++n] = t[i];
  }
  void addr(char* t, int L) {
    for (int i = 1; i <= L; i++) s[++n] = t[L - i];
  }
  void addw(char* t, int L) {
    int l = 0, r = L - 1;
    while (l < r && t[l] == t[r]) l++, r--;
    t[l] < t[r] ? addl(t, L) : addr(t, L);
  }
  bool operator<(const ele& a) const {
    for (int i = 1; i <= n; i++)
      if (s[i] ^ a.s[i]) return s[i] < a.s[i];
  }
  void print() { puts(s + 1); }
} ans0, ans1, ans2;
void ini() {
  for (int i = 1, j, k; i <= n;) {
    for (k = (j = i) + 1; k <= n && T[j] <= T[k]; k++)
      j = T[j] ^ T[k] ? i : j + 1;
    for (fk[++m] = i, le[m] = k - j; i <= j; i += k - j)
      ;
  }
  fk[m + 1] = n + 1;
  nx[1] = n;
  int x = 0, y = 0;
  for (int i = 2; i <= n; i++) {
    if (i <= y) nx[i] = min(nx[i - x + 1], y - i + 1);
    while (i + nx[i] <= n && T[1 + nx[i]] == T[i + nx[i]]) nx[i]++;
    if (i + nx[i] >= y) x = i, y = i + nx[i] - 1;
  }
  x = y = 0;
  for (int i = 1; i <= n; i++) {
    if (i <= y) ex[i] = min(nx[i - x + 1], y - i + 1);
    while (i + ex[i] <= n && T[1 + ex[i]] == S[i + ex[i]]) ex[i]++;
    if (i + ex[i] >= y) x = i, y = i + ex[i] - 1;
  }
}
bool cmp(int x, int y) {
  if (ex[n - y + 2] < y - x) return y = n - y + 2, S[y + ex[y]] > T[1 + ex[y]];
  return y = y - x + 1, T[1 + nx[y]] > T[y + nx[y]];
}
void work2() {
  ans1 = ans0;
  ans2 = ans0;
  ans0.addl(T + fk[m - 0], fk[m + 1] - fk[m - 0]);
  ans0.addw(T + 1, fk[m - 0] - 1);
  ans1.addl(T + fk[m - 1], fk[m + 1] - fk[m - 1]);
  ans1.addw(T + 1, fk[m - 1] - 1);
  int o = 1;
  for (int i = 2; i <= fk[m + 1] - 1; i++)
    if (cmp(o, i)) o = i;
  ans2.addr(T + o, fk[m + 1] - o);
  ans2.addl(T + 1, o - 1);
  if (ans1 < ans0) ans0 = ans1;
  if (ans2 < ans0) ans0 = ans2;
}
int main() {
  scanf("%s%d", S + 1, &K);
  n = strlen(S + 1);
  for (int i = 1; i <= n; i++) T[i] = S[n - i + 1];
  ini();
  for (; K > 2 && m; K--) {
    if (le[m] == 1)
      for (; m && le[m] == 1; m--) ans0.addl(T + fk[m], fk[m + 1] - fk[m]);
    else
      ans0.addl(T + fk[m], fk[m + 1] - fk[m]), m--;
  }
  if (K == 1 || m <= 1)
    ans0.addw(T + 1, fk[m + 1] - 1);
  else
    work2();
  ans0.print();
}