#include<bits/stdc++.h>
using namespace std;
#define int long long
using ll = long long;
#define xx first
#define yy second
mt19937 rnd(std::chrono::high_resolution_clock::now().time_since_epoch().count());
ll modpow(ll a, ll b, ll mod) {
ll res = 1;
while (b > 0) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return (res + mod) % mod;
}
bool isPrime(ll n) {
if (n < 2) return false;
if (n == 2 || n == 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (ll i = 5; i * i <= n; i += 6) if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
ll mod1 = 127657753, mod2 = 987654319, p1 = 31, p2 = 37;
ll invp1 = modpow(p1, mod1 - 2, mod1), invp2 = modpow(p2, mod2 - 2, mod2);
void randomize() { // Randomize the mod and base
mod1 = rnd() % 100000000 + 900000000, mod2 = rnd() % 100000000 + 900000000;
while (!isPrime(mod1)) mod1++;
while (!isPrime(mod2) || mod1 == mod2) mod2++;
p1 = rnd() % 100 + 31, p2 = rnd() % 100 + 37;
while (!isPrime(p1)) p1++;
while (!isPrime(p2) || p1 == p2) p2++;
invp1 = modpow(p1, mod1 - 2, mod1), invp2 = modpow(p2, mod2 - 2, mod2);
}
struct Hash {
string s;
int n = 0;
vector<pair<ll, ll>> sHash, pPow, invpPow;
void setVal(string& S) { // Set the string
s = S, n = S.size();
}
void genP() { // Generate powers of p and invp for faster queries
pPow.resize(n + 1, {1, 1});
invpPow.resize(n + 1, {1, 1});
for (int i = 1; i <= n; i++) {
pPow[i].xx = pPow[i - 1].xx * p1 % mod1;
pPow[i].yy = pPow[i - 1].yy * p2 % mod2;
invpPow[i].xx = invpPow[i - 1].xx * invp1 % mod1;
invpPow[i].yy = invpPow[i - 1].yy * invp2 % mod2;
}
}
void genHash() { // 0-indexed
sHash.resize(n + 1, {0, 0});
for (int i = 0; i < n; i++) {
sHash[i + 1].xx = (sHash[i].xx + (s[i] - 'a' + 1) * pPow[i].xx) % mod1;
sHash[i + 1].yy = (sHash[i].yy + (s[i] - 'a' + 1) * pPow[i].yy) % mod2;
}
}
pair<ll, ll> getHash(int l, int r) { // 0-indexed
ll h1 = (sHash[r + 1].xx - sHash[l].xx + mod1) % mod1 * invpPow[l].xx % mod1;
ll h2 = (sHash[r + 1].yy - sHash[l].yy + mod2) % mod2 * invpPow[l].yy % mod2;
return {h1, h2};
}
pair<ll, ll> getHash() { // Get the hash of the whole string
return getHash(0, n - 1);
}
void doAll(string& S) {
setVal(S), genP(), genHash();
}
};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s;
cin >> s;
int n = s.size();
s += s;
Hash h;
h.doAll(s);
int mx = 0;
for(int i = 1; i < n; i++){
int lo = 0, hi = n - 1, res = -1;
while(lo <= hi){
int mid = (lo + hi) / 2;
if(h.getHash(0 + mx, mid + mx) == h.getHash(0 + i, mid + i)) lo = mid + 1;
else {
hi = mid - 1;
res = mid;
}
}
if(res != -1 and s[0 + mx + res] > s[0 + i + res]) mx = i;
}
cout << s.substr(mx, n - mx);
cout << s.substr(0, mx);
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBpbnQgbG9uZyBsb25nCiAKdXNpbmcgbGwgPSBsb25nIGxvbmc7CiNkZWZpbmUgeHggZmlyc3QKI2RlZmluZSB5eSBzZWNvbmQKbXQxOTkzNyBybmQoc3RkOjpjaHJvbm86OmhpZ2hfcmVzb2x1dGlvbl9jbG9jazo6bm93KCkudGltZV9zaW5jZV9lcG9jaCgpLmNvdW50KCkpOwogCmxsIG1vZHBvdyhsbCBhLCBsbCBiLCBsbCBtb2QpIHsKICAgIGxsIHJlcyA9IDE7CiAgICB3aGlsZSAoYiA+IDApIHsKICAgICAgICBpZiAoYiAmIDEpIHJlcyA9IHJlcyAqIGEgJSBtb2Q7CiAgICAgICAgYSA9IGEgKiBhICUgbW9kOwogICAgICAgIGIgPj49IDE7CiAgICB9CiAgICByZXR1cm4gK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ICAgICAgICAgIGlmKGguZ2V0SGFzaCgwICsgbXgsIG1pZCArIG14KSA9PSBoLmdldEhhc2goMCArIGksIG1pZCArIGkpKSBsbyA9IG1pZCArIDE7CiAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgaGkgPSBtaWQgLSAxOwogICAgICAgICAgICAgICAgcmVzID0gbWlkOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGlmKHJlcyAhPSAtMSBhbmQgc1swICsgbXggKyByZXNdID4gc1swICsgaSArIHJlc10pIG14ID0gaTsKICAgIH0KICAgIGNvdXQgPDwgcy5zdWJzdHIobXgsIG4gLSBteCk7CiAgICBjb3V0IDw8IHMuc3Vic3RyKDAsIG14KTsKICAgIHJldHVybiAwOwp9