import re


def roman_numeral(s):
	""" Returns a roman numeral converted to an integer if valid, or the source string """
	def roman_to_int(s):
		d = {'m': 1000, 'd': 500, 'c': 100, 'l': 50, 'x': 10, 'v': 5, 'i': 1}
		n = [d[i] for i in s if i in d]
		return str(sum([i if i>=n[min(j+1, len(n)-1)] else -i for j,i in enumerate(n)]))

	if not re.match(r'^(?=[mdclxvi])m*(c[md]|d?c{0,3})(x[cl]|l?x{0,3})(i[xv]|v?i{0,3})$', s):
		return s

	return roman_to_int(s)