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;
char s[5000005], sr[5000005], srs[2 * 5000005], ans[5000005], a[5000005],
    b[5000005];
char *ans1 = a, *tmp = b;
int bst, N, u, hd, n, i, kp, k, j, pre, tot = 0, seq[5000005], bx[2 * 5000005],
                                        ty[2 * 5000005], alr = 0;
void output(char *pos, int x) {
  for (int i = 0; i < x; i++) printf("%c", *(pos + i));
}
char *mymin(char *x, char *y, int l) {
  int i;
  for (i = 0; i < l; i++)
    if ((*(x + i)) != (*(y + i))) break;
  if (*(x + i) < *(y + i)) return x;
  return y;
}
void in(char *pos1, char *pos2, int x) {
  for (int i = 0; i < x; i++) *(pos1 + i) = *(pos2 + i);
}
void inrev(char *pos1, char *pos2, int x) {
  for (int i = 0; i < x; i++) *(pos1 + i) = *(pos2 - i);
}
int len(int p) { return seq[p] - seq[p - 1]; }
int cmp(char *pos1, char *pos2, int x) {
  int i;
  for (i = 0; i < x; i++)
    if ((*(pos1 + i)) != (*(pos2 + i))) break;
  if (i == x) return 0;
  if (*(pos1 + i) < *(pos2 + i)) return -1;
  return 1;
}
int main() {
  scanf("%s", s + 1);
  n = N = strlen(s + 1);
  for (i = 1; i <= n; i++) sr[n - i + 1] = s[i];
  scanf("%d", &kp);
  if (kp == 1) {
    output(mymin(s + 1, sr + 1, n), n);
    return 0;
  }
  for (i = 1; i <= n;) {
    j = i;
    k = i + 1;
    while (k <= n && sr[k] >= sr[j]) {
      if (sr[k] > sr[j])
        j = i;
      else
        j++;
      k++;
    }
    while (i <= j) {
      seq[++tot] = i + k - j - 1;
      i += k - j;
    }
  }
  hd = tot;
  while (kp > 2 && hd) {
    pre = hd;
    if (len(hd) == 1) {
      while (hd && len(hd) == 1) hd--;
      inrev(ans + alr + 1, sr + seq[pre], seq[pre] - seq[hd]);
      alr += seq[pre] - seq[hd];
    } else {
      while (hd && len(hd) == len(pre) &&
             cmp(sr + seq[hd - 1] + 1, sr + seq[pre - 1] + 1, len(pre)) == 0)
        hd--;
      in(ans + alr + 1, sr + seq[hd] + 1, seq[pre] - seq[hd]);
      alr += seq[pre] - seq[hd];
    }
    kp--;
  }
  output(ans + 1, alr);
  if (hd == 0) return 0;
  n = seq[hd];
  for (i = 1; i <= n; i++) sr[n + i] = sr[i];
  i = j = 1;
  while (j < n) {
    for (k = 0; k < n; k++)
      if (sr[i + k] != sr[j + 1 + k]) break;
    if (k == n) {
      in(ans1 + 1, sr + i, n);
      goto tag;
    }
    if (sr[i + k] < sr[j + 1 + k])
      j = j + 1 + k;
    else
      i = max(j + 1, i + k + 1), j = i;
  }
  in(ans1 + 1, sr + i, n);
tag:;
  for (i = 1; i <= n; i++) srs[i] = srs[n + n - i + 1] = sr[i];
  j = k = 1;
  for (i = 2; i <= 2 * n; i++) {
    if (i <= k && bx[i - j + 1] < k - j + 1)
      bx[i] = j - 1 + bx[i - j + 1];
    else {
      j = i;
      bx[i] = max(k, i - 1);
      while (bx[i] < 2 * n && srs[bx[i] + 1] == srs[bx[i] + 2 - i])
        bx[i]++, k++;
      if (bx[i] == 2 * n || srs[bx[i] + 1] > srs[bx[i] + 2 - i])
        ty[i] = 1;
      else
        ty[i] = 0;
    }
  }
  bst = 1;
  for (i = 2; i <= n; i++) {
    if (bx[n + bst + 1] < n + i) {
      if (ty[n + bst + 1] == 0) bst = i;
      continue;
    }
    if (ty[i - bst + 1] == 1) bst = i;
  }
  inrev(tmp + 1, sr + n, bst);
  in(tmp + bst + 1, sr + 1, n - bst);
  if (cmp(ans1 + 1, tmp + 1, n) == 1) swap(ans1, tmp);
  while (hd > 0) {
    pre = hd;
    for (i = hd; i > 0 && len(i) == len(pre) &&
                 cmp(sr + seq[i - 1] + 1, sr + seq[pre - 1] + 1, len(pre)) == 0;
         i--)
      ;
    hd = i;
    if (seq[pre] - seq[hd] <= n - seq[pre]) break;
    if (cmp(sr + seq[hd] + 1, sr + seq[pre] + 1, n - seq[pre]) != 0) break;
    if (cmp(sr + seq[hd] + n - seq[pre] + 1, s + N - seq[pre] + 1,
            seq[pre] - seq[hd]) == 1)
      break;
  }
  in(tmp + 1, sr + seq[pre] + 1, n - seq[pre]);
  inrev(tmp + n - seq[pre] + 1, sr + seq[pre], seq[pre]);
  if (cmp(ans1 + 1, tmp + 1, n) == 1) swap(ans1, tmp);
  output(ans1 + 1, n);
  return 0;
}