static long find_closest_palindrome(long x) {
		// Make str a palindrome by mirroring the left half to the right half.
		String mirrored = mirrorLeftHalf(String.valueOf(x));
		StringBuilder str = new StringBuilder(mirrored);
	  
		long mirror_left = Long.valueOf(mirrored);
		int idx = (str.length() - 1) >> 1;
		if (mirror_left >= x) {
			// Subtract one from the left half.
			while (idx >= 0) {
				if (str.charAt(idx) == '0') {
					str.setCharAt(idx--, '9');
				} else {
					char c = str.charAt(idx);
					str.setCharAt(idx, (char) (c - 1));
					break;
				}
			}
			if (str.charAt(0) == '0') { // special case, make the whole string as "99...9".
				str.deleteCharAt(0); // removes the leading 0.
				fill(str, '9');
			}
		} else { // mirror_left < x.
			// Add one to the left half.
			while (idx >= 0) {
				if (str.charAt(idx) == '9') {
					str.setCharAt(idx--, '0');
				} else {
					char c = str.charAt(idx);
					str.setCharAt(idx, (char) (c + 1));
					break;
				}
			}
		}

	  // Make str a palindrome again by mirroring the left half to the right half.
	  mirrored = mirrorLeftHalf(str.toString());
	  return abs(x - mirror_left) < abs(x - Long.valueOf(mirrored)) ?
	              mirror_left : Long.valueOf(mirrored);
	}