/*
 * Copyright (C) 2012, 2016  Hiroyasu Kamo
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * ACM-ICPC 2012 Japan Domestic Contest
 *  Problem B  Recurring Decimals
 *
 *	Author: Hiroyasu Kamo <wd@ics.nara-wu.ac.jp>
 */

#include <algorithm>
#include <utility>
#include <functional>
#include <iterator>
#include <vector>
#include <iostream>
using std::find;
using std::sort;
using std::less;
using std::distance;
using std::vector;
using std::cin;
using std::cout;

class rd {
private:
	int	base;
	int	len;
	vector<int>	to_digits(int) const;
	template<typename Iter>
	int	from_digits(Iter, Iter) const;
public:
	rd(int l, int b) : base(b), len(l) {}
	int	operator()(int) const;
};

inline vector<int>
rd::to_digits(int x) const
{
	vector<int>	digits;
	for (int i = 0; i < len; i ++) {
		digits.push_back(x % base);
		x /= base;
	}
	sort(digits.begin(), digits.end());
	return std::move(digits);
}

template<typename Iter>
inline int
rd::from_digits(Iter first, Iter last) const
{
	int	x = 0;
	for (; first != last; ++ first) {
		x = x * base + *first;
	}
	return x;
}

int
rd::operator()(int x) const
{
	vector<int>	digits = to_digits(x);
	return from_digits(digits.rbegin(), digits.rend()) - from_digits(digits.begin(), digits.end());
}

int
main()
{
	for (int a0, l; cin >> a0 >> l && !(a0 == 0 && l == 0); ) {
		const rd	f(l, 10);
		for (vector<int> a = {a0} ;;) {
			const int	an = f(a.back());
			const auto	p = find(a.begin(), a.end(), an);
			if (p != a.end()) {
				cout << distance(a.begin(), p) << " "
				     << an << " "
				     << distance(p, a.end()) << "\n";
				break;
			}
			a.push_back(an);
		}
	}
	return 0;
}
