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 maxn = 10000005;
char s[maxn];
int p[maxn], f[maxn], k, n, m;
void factorize() {
  for (int i = 1, j, k; i <= n;) {
    k = i, j = i + 1;
    for (; j <= n && s[k] <= s[j]; ++j)
      if (s[k] < s[j])
        k = i;
      else
        ++k;
    p[++m] = i;
    i += (j - i) / (j - k) * (j - k);
  }
}
void print(char *s, char *t) {
  bool rev = 0;
  for (char *h = s, *g = t; h <= t; h++, g--)
    if ((*g) != (*h)) {
      if ((*g) < (*h)) rev = 1;
      break;
    }
  if (rev) {
    for (; t >= s; t--) putchar(*t);
  } else
    for (; s <= t; s++) putchar(*s);
}
void ex_kmp(int len) {
  for (int i = 2, p = 0; i <= len; i++) {
    if (i <= p + f[p] - 1) f[i] = min(p + f[p] - i, f[i - p + 1]);
    for (; i + f[i] <= len && s[f[i] + 1] == s[i + f[i]]; ++f[i])
      ;
    if (i + f[i] > p + f[p]) p = i;
  }
  f[1] = len;
}
bool small(char *s, char *t, int l) {
  for (; l; l--, s++, t++)
    if ((*s) < (*t))
      return 1;
    else if ((*s) > (*t))
      return 0;
  return 0;
}
void test(char *s, char *t) {
  if (s > t) return;
  bool rev = 0;
  for (char *h = s, *g = t; h <= t; h++, g--)
    if ((*g) != (*h)) {
      if ((*g) < (*h)) rev = 1;
      break;
    }
  if (!rev) reverse(s, t + 1);
}
int main() {
  gets(s + 1);
  scanf("%d", &k);
  n = strlen(s + 1);
  reverse(s + 1, s + n + 1);
  if (k == 1)
    print(s + 1, s + n);
  else {
    factorize();
    if (k > 2) {
      if (s[p[m]] == s[n]) {
        for (; m > 1 && s[p[m]] == s[n]; m--)
          print(s + p[m], s + n), n = p[m] - 1;
        k--;
      }
      for (; m > 1 && k > 2; m--, k--) print(s + p[m], s + n), n = p[m] - 1;
    }
    if (m == 1)
      print(s + p[1], s + n);
    else {
      static char bx[maxn], by[maxn], bs[maxn];
      (memcpy(bs, s, sizeof bs));
      reverse(bs + p[m], bs + n + 1);
      test(bs + 1, bs + p[m] - 1);
      (memcpy(bx, bs, sizeof bx));
      reverse(bx + 1, bx + n + 1);
      (memcpy(bs, s, sizeof bs));
      reverse(bs + p[m - 1], bs + n + 1);
      test(bs + 1, bs + p[m - 1] - 1);
      (memcpy(by, bs, sizeof by));
      reverse(by + 1, by + n + 1);
      if (small(by + 1, bx + 1, n)) (memcpy(bx, by, sizeof bx));
      for (int i = n + 1; i <= 2 * n; i++) s[i] = s[n - (i - n - 1)];
      ex_kmp(2 * n);
      int j = 0;
      for (int i = 1; i < n; i++) {
        if (f[n + j + 1] < i - j) {
          if (s[n + j + 1 + f[n + j + 1]] < s[f[n + j + 1] + 1]) j = i;
        } else if (f[i - j + 1] < n - i)
          if (s[f[i - j + 1] + 1] < s[i - j + 1 + f[i - j + 1]]) j = i;
      }
      reverse(s + 1, s + n + 1);
      for (int i = n + 1; i <= 2 * n; i++) s[i] = 0;
      reverse(s + j + 1, s + n + 1);
      (memcpy(by, s, sizeof by));
      if (small(by + 1, bx + 1, n)) (memcpy(bx, by, sizeof bx));
      for (int i = 1; i <= n; i++) putchar(bx[i]);
    }
  }
  puts("");
  return 0;
}