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;
int ri() {
  int x;
  scanf("%d", &x);
  return x;
}
const int N = 5000000;
char a[2 * N + 1], x[N + 1], y[N + 1];
int z[2 * N + 1], c[N];
void prefixes(int n) {
  z[0] = n;
  for (int f = 0, g = 0, i = 1; i < n; i++)
    if (i < g && z[i - f] != g - i)
      z[i] = min(z[i - f], g - i);
    else {
      f = i;
      g = max(g, f);
      for (; g < n && a[g] == a[g - f]; g++)
        ;
      z[i] = g - f;
    }
}
int duval(int n) {
  int m = 0, s = 0;
  while (s < n) {
    int i = s, j = s + 1;
    while (j < n && a[i] <= a[j]) {
      if (a[i] < a[j])
        i = s;
      else
        i++;
      j++;
    }
    c[m++] = s;
    s += (j - s) / (j - i) * (j - i);
  }
  return m;
}
bool cmp(int i, int j) {
  for (; i < --j && a[i] == a[j]; i++)
    ;
  return a[i] <= a[j];
}
void print(int i, int j) {
  if (cmp(i, j))
    while (i < j) putchar(a[i++]);
  else
    while (i < j) putchar(a[--j]);
}
void cp(int n, int j, char x[]) {
  if (cmp(0, j))
    copy(a, a + j, x + n - j);
  else
    reverse_copy(a, a + j, x + n - j);
}
int main() {
  gets(a);
  int n = strlen(a), k = ri();
  if (k == 1)
    print(0, n);
  else {
    reverse(a, a + n);
    int m = duval(n);
    if (k > 2) {
      if (a[c[m - 1]] == a[n - 1]) {
        while (m && a[c[m - 1]] == a[n - 1]) {
          m--;
          for (int i = (c[m]); i < (n); i++) putchar(a[i]);
          n = c[m];
        }
        k--;
      }
      for (; k > 2 && m; k--) {
        m--;
        for (int i = (c[m]); i < (n); i++) putchar(a[i]);
        n = c[m];
      }
    }
    if (m == 1)
      print(0, n);
    else if (m > 1) {
      copy(a + c[m - 1], a + n, x);
      cp(n, c[m - 1], x);
      copy(a + c[m - 2], a + n, y);
      cp(n, c[m - 2], y);
      if (strcmp(y, x) < 0) strcpy(x, y);
      a[n] = '\0';
      reverse_copy(a, a + n, a + n + 1);
      prefixes(2 * n + 1);
      int opt = 0;
      for (int i = (1); i < (n - 1); i++) {
        int mis = z[2 * n - i];
        if (mis < i - opt) {
          if (a[mis] < a[2 * n - (i - mis)]) opt = i;
        } else {
          mis = z[i - opt];
          if (mis < opt + 1 && a[i - opt + mis] < a[mis]) opt = i;
        }
      }
      reverse_copy(a + opt + 1, a + n, y);
      copy(a, a + opt + 1, y + n - opt - 1);
      printf("%s", strcmp(y, x) < 0 ? y : x);
    }
  }
  puts("");
}